Skip to content

Instantly share code, notes, and snippets.

@dyerrington
Last active November 30, 2015 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyerrington/d8c4fd4b768c4a57bb29 to your computer and use it in GitHub Desktop.
Save dyerrington/d8c4fd4b768c4a57bb29 to your computer and use it in GitHub Desktop.
When I work with date formats, it’s nice to have them as actual “datetime” objects rather than objects. If you notice when you first import the csv, the “Time” feature has a dtype “object”. If we convert this object to a “datetime” type, we can use Pandas Grouper() to actually do a groupby unique time period (ie: days, weeks, months, years).
#Step1, convert Time after loading:
ufo = pd.read_csv('https://raw.githubusercontent.com/sinanuozdemir/SF_DAT_17/master/data/ufo.csv') # can also read csvs directly from the web!
ufo['Time'] = ufo['Time'].apply(pd.to_datetime)
# Step 2: Group by unique days
ufo.groupby([pd.Grouper(key='Time',freq='1D')])[['Shape Reported']].count()
# Also, you can concat Year, Month, and Day into a new feature, and group by that. As an engineer, I much prefer to work on strict types and leverage current method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment