Python uses Pandas to process dates and times

When working with data, time series data is often encountered. Pandas is a very useful tool when working with time series data.

Pandas offers a different set of tools through which we can perform all the necessary tasks with datetime data. Let’s try to understand with the examples discussed below.

Code 1: Create a date data frame

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

The output is as follows:

Code 2: Create a date range and display the basic features

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

The output is as follows:

(9, 2018)

Appointment time

Datetime features can be divided into two categories. The first time point is within a time period and the second time point is after a specific time period. These features are useful for understanding patterns in your data.

Divide a given date into features-

panda.series.dt.year returns the year of the datetime.

pandas. Series.dt.month returns the month of the datetime.

pandas. Series.dt.day returns the date of the datetime.

pandas. Series.dt.hour returns the hours of the datetime.

pandas. Series.dt.minute returns the minute of the datetime.

Recommend all data time from the property here.

Code 3: Break down data and time into separate functions

# 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 )

The output is as follows:

Code 4:

To get the current time, use Timestamp.now(), then convert the timestamp to a datetime, and get direct access to the year, month, or day.

# Input present datetime using Timestamp
t = pandas.tslib.Timestamp.now()
t
Timestamp('2018-09-18 17:18:49.101496')
# Convert timestamp to datetime
t.to_datetime()
datetime.datetime(2018, 9, 18, 17, 18, 49, 101496)
# Directly access and print the features
t.year
t.month
t.day
t.hour
t.minute
t.second
2018
8
25
15
53

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

import pandas as pd
  
url = 'http://bit.ly/uforeports'
  
# read csv file
df = pd.read_csv(url)           
df.head()

The output is as follows:

# Convert the Time column to datatime format
df[ 'Time' ] = pd.to_datetime(df.Time)
  
df.head()
# shows the type of each column data
df.dtypes
City                       object
Colors Reported            object
Shape Reported             object
State                      object
Time               datetime64[ns]
dtype: object
# Get hour detail from time data
df.Time.dt.hour.head()
0    22
1    20
2    14
3    13
4    19
Name: Time, dtype: int64
# Get name of each date
df.Time.dt.weekday_name.head()
0     Sunday
1     Monday
2     Sunday
3     Monday
4    Tuesday
Name: Time, dtype: object
# Get ordinal day of the year
df.Time.dt.dayofyear.head()
0    152
1    181
2     46
3    152
4    108
Name: Time, dtype: int64

First, your interview preparation enhances your data structure concepts with the Python DS course.