Python | Working with date and time using Pandas

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

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd
  
# Create dates dataframe with frequency  
data = pd.date_range('1/1/2011', periods = 10, freq ='H')
  
data

chevron_right


Output:

 

Code #2: Create range of dates and show basic features

filter_none

edit
close

play_arrow

link
brightness_4
code

# Create date and time with dataframe
data = pd.date_range('1/1/2011', periods = 10, freq ='H')
  
x = datetime.now()
x.month, x.year

chevron_right


Output:

(9, 2018)

 
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.

Refer all datatime properties from here.

Code #3: Break data and time into separate features

filter_none

edit
close

play_arrow

link
brightness_4
code

# Create date and time with dataframe
rng = pd.DataFrame()
rng['date'] = pd.date_range('1/1/2011', periods = 72, freq ='H')
  
# Print the dates in dd-mm-yy format
rng[:5]
  
# Create features for year, month, day, hour, and minute
rng['year'] = rng['date'].dt.year
rng['month'] = rng['date'].dt.month
rng['day'] = rng['date'].dt.day
rng['hour'] = rng['date'].dt.hour
rng['minute'] = rng['date'].dt.minute
  
# Print the dates divided into features
rng.head(3)

chevron_right


Output:

 
Code #4: To get the present time, use Timestamp.now() and then convert timestamp to datetime and directly access year, month or day.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Input present datetime using Timestamp
t = pandas.tslib.Timestamp.now()
t

chevron_right


Timestamp('2018-09-18 17:18:49.101496')

 

filter_none

edit
close

play_arrow

link
brightness_4
code

# Convert timestamp to datetime
t.to_datetime()

chevron_right


datetime.datetime(2018, 9, 18, 17, 18, 49, 101496)

 

filter_none

edit
close

play_arrow

link
brightness_4
code

# Directly access and print the features
t.year
t.month
t.day
t.hour
t.minute
t.second

chevron_right


2018
8
25
15
53



Let’s analyze this problem on a real dataset uforeports.

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd
  
  
# read csv file
df = pd.read_csv(url)           
df.head()

chevron_right


Output:

 

filter_none

edit
close

play_arrow

link
brightness_4
code

# Convert the Time column to datatime format
df['Time'] = pd.to_datetime(df.Time)
  
df.head()

chevron_right



 

filter_none

edit
close

play_arrow

link
brightness_4
code

# shows the type of each column data
df.dtypes

chevron_right


City                       object
Colors Reported            object
Shape Reported             object
State                      object
Time               datetime64[ns]
dtype: object

 

filter_none

edit
close

play_arrow

link
brightness_4
code

# Get hour detail from time data
df.Time.dt.hour.head()

chevron_right


0    22
1    20
2    14
3    13
4    19
Name: Time, dtype: int64

 

filter_none

edit
close

play_arrow

link
brightness_4
code

# Get name of each date
df.Time.dt.weekday_name.head()

chevron_right


0     Sunday
1     Monday
2     Sunday
3     Monday
4    Tuesday
Name: Time, dtype: object

 

filter_none

edit
close

play_arrow

link
brightness_4
code

# Get ordinal day of the year
df.Time.dt.dayofyear.head()

chevron_right


0    152
1    181
2     46
3    152
4    108
Name: Time, dtype: int64


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai