While working with data, encountering time series data is very usual. Pandas is a very useful tool while working with time series data.
Pandas provide a different set of tools using which we can perform all the necessary tasks on date-time data. Let’s try to understand with the examples discussed below.
Code #1: Create a dates dataframe
Code #2: Create range of dates and show basic features
Datetime features can be divided into two categories.The first one time moments in a period and second the time passed since a particular period. These features can be very useful to understand the patterns in the data.
Divide a given date into features –
pandas.Series.dt.year returns the year of the date time.
pandas.Series.dt.month returns the month of the date time.
pandas.Series.dt.day returns the day of the date time.
pandas.Series.dt.hour returns the hour of the date time.
pandas.Series.dt.minute returns the minute of the date time.
datatime properties from here.
Code #3: Break data and time into separate features
Code #4: To get the present time, use Timestamp.now() and then convert timestamp to datetime and directly access year, month or day.
datetime.datetime(2018, 9, 18, 17, 18, 49, 101496)
2018 8 25 15 53
Let’s analyze this problem on a real dataset uforeports.
City object Colors Reported object Shape Reported object State object Time datetime64[ns] dtype: object
0 22 1 20 2 14 3 13 4 19 Name: Time, dtype: int64
0 Sunday 1 Monday 2 Sunday 3 Monday 4 Tuesday Name: Time, dtype: object
0 152 1 181 2 46 3 152 4 108 Name: Time, dtype: int64
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course