Date and Time in Pandas¶
Generate a dummy date series¶
We can use the date_range() function of pandas. Option period specifies the number of enteries to be generated. And option freq specifies the date and time frequency, where :
D = Daily Data
M = Monthly Data
MS = Month Start dates
B = Business Dates
Access the pandas documentation at this page for date and time https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.date_range.html
In [6]:
import pandas as pd
range = pd.date_range('2002 July 15 10:15', periods = 10, freq = 'M')
In [7]:
range
Out[7]:
Create dates from July 15, 2016 to July 24, 2016¶
In [15]:
range = pd.date_range(start = '2016 july 15', end = '2016 july 24', freq = 'D')
In [16]:
range
Out[16]:
How many dates are in the above range¶
len() return the number of items in a container.
In [19]:
len(range)
Out[19]:
Which time formats pandas will not accept¶
'2016 Jul 1' , '7/1/2016', '1/7/2016', 'July 1, 2016', '2016-07-01'
In [21]:
pew = pd.read_csv('https://opendoors.pk/wp-content/uploads/2020/02/pew.csv')
In [22]:
pew.head()
Out[22]:
CONVERT STRING TO DATE TIME FORMAT¶
In [23]:
# Use the ufo dataset from the web
ufo = pd.read_csv('http://bit.ly/uforeports')
In [24]:
ufo.head()
Out[24]:
In [25]:
# Find the characteristics of the variables in the dataset
ufo.dtypes
Out[25]:
In [26]:
# As we can se that the Time column is an object, meaning it is a text column
Convert string to date time¶
We can use the top level function to_datetime() of the pandas library
In [27]:
# Replace the existing Time column
ufo['Time'] = pd.to_datetime(ufo['Time'])
In [28]:
ufo.head()
Out[28]:
In [29]:
ufo.dtypes
Out[29]:
What are the benefits of datatime format¶
1. We can pull out hours now¶
Now we can use the top level dt function and all other low level time functions. To get hours from the column, add dt.hour
In [31]:
hours = ufo['Time'].dt.hour
In [32]:
hours.head()
Out[32]:
2. Get Weekdays Names¶
In [34]:
weekedays = ufo['Time'].dt.weekday_name
In [35]:
weekedays.head()
Out[35]:
3. Get Weekdays Numbers¶
In [38]:
weekedaysn = ufo['Time'].dt.weekday
In [39]:
weekedaysn.head()
Out[39]:
4. Day of the year¶
In [40]:
dofy = ufo['Time'].dt.dayofyear
In [41]:
dofy.head()
Out[41]:
5. Year¶
In [43]:
year = ufo['Time'].dt.year
In [44]:
year.head()
Out[44]:
6. Month¶
In [45]:
month = ufo['Time'].dt.month
In [46]:
month.head()
Out[46]:
Full Reference¶
Function | Purpose |
Series.dt.date | Returns numpy array of python datetime.date objects (namely, the date part of Timestamps without timezone information). |
Series.dt.time | Returns numpy array of datetime.time. |
Series.dt.timetz | Returns numpy array of datetime.time also containing timezone information. |
Series.dt.year | The year of the datetime. |
Series.dt.month | The month as January=1, December=12. |
Series.dt.day | The days of the datetime. |
Series.dt.hour | The hours of the datetime. |
Series.dt.minute | The minutes of the datetime. |
Series.dt.second | The seconds of the datetime. |
Series.dt.microsecond | The microseconds of the datetime. |
Series.dt.nanosecond | The nanoseconds of the datetime. |
Series.dt.week | The week ordinal of the year. |
Series.dt.weekofyear | The week ordinal of the year. |
Series.dt.dayofweek | The day of the week with Monday=0, Sunday=6. |
Series.dt.weekday | The day of the week with Monday=0, Sunday=6. |
Series.dt.dayofyear | The ordinal day of the year. |
Series.dt.quarter | The quarter of the date. |
Series.dt.is_month_start | Indicates whether the date is the first day of the month. |
Series.dt.is_month_end | Indicates whether the date is the last day of the month. |
Series.dt.is_quarter_start | Indicator for whether the date is the first day of a quarter. |
Series.dt.is_quarter_end | Indicator for whether the date is the last day of a quarter. |
Series.dt.is_year_start | Indicate whether the date is the first day of a year. |
Series.dt.is_year_end | Indicate whether the date is the last day of the year. |
Series.dt.is_leap_year | Boolean indicator if the date belongs to a leap year. |
Series.dt.daysinmonth | The number of days in the month. |
Series.dt.days_in_month | The number of days in the month. |
Series.dt.tz | Return timezone, if any. |
Series.dt.freq | |
In [ ]: