Working with date and time in pandas
This introduction to pandas is derived from Data School's pandas Q&A with my own notes and code.
Working with dates and times in pandas¶
In [1]:
import pandas as pd
In [2]:
url = 'http://bit.ly/uforeports'
ufo = pd.read_csv(url)
In [3]:
ufo.head()
Out[3]:
In [4]:
ufo.dtypes
Out[4]:
In [10]:
# 5 characters from the end to 3 characters from the end
# this is quite a chore
ufo.Time.str.slice(-5, -3).astype(int).head()
Out[10]:
In [14]:
# we will convert the Time column to datatime format
# there are many options to ensure this works well with your data
ufo['Time'] = pd.to_datetime(ufo.Time)
ufo.head()
Out[14]:
In [12]:
ufo.dtypes
Out[12]:
In [16]:
ufo.Time.dt.hour.head()
Out[16]:
In [18]:
ufo.Time.dt.weekday_name.head()
Out[18]:
In [19]:
ufo.Time.dt.weekday.head()
Out[19]:
In [20]:
ufo.Time.dt.dayofyear.head()
Out[20]:
Timestamps
We can use it for comparison and mathematical operations
In [22]:
ts = pd.to_datetime('1/1/1999')
In [25]:
ufo.loc[ufo.Time >= ts, :].head()
Out[25]:
In [26]:
ufo.Time.max()
Out[26]:
In [27]:
ufo.Time.max() - ufo.Time.min()
Out[27]:
In [28]:
(ufo.Time.max() - ufo.Time.min()).days
Out[28]:
Plotting
In [29]:
%matplotlib inline
In [30]:
ufo['Year'] = ufo.Time.dt.year
In [31]:
ufo.head()
Out[31]:
In [39]:
ufo.Year.value_counts().sort_index().plot()
Out[39]: