![]() |
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. Working with Dates in PandasThe date class in the DateTime module of Python deals with dates in the Gregorian calendar. It accepts three integer arguments: year, month, and day. Python3
Output: 2000-09-17
<class 'datetime.date'>
Year, month, and day extractionRetrieve the year, month, and day components from a Timestamp object. Python3
Output: 2023
10
4
Weekdays and quartersDetermine the weekday and quarter associated with a Timestamp. Python3
Output: 15
30
2
4
Working with Time in PandasAnother class in the DateTime module is called time, which returns a DateTime object and takes integer arguments for time intervals up to microseconds: Python3
Output: 12:50:12.000040
<class 'datetime.time'>
Time periods and date offsetsCreate custom time periods and date offsets for flexible date manipulation. Python3
Output: 2023
10
4
2026-01-14 15:30:00
Handling Time ZonesTime zones play a crucial role in date and time data. Pandas provides mechanisms to handle time zones effectively:
Python3
Output: Original Timestamp: 2023-10-04 15:30:00-04:00
UTC Timestamp: 2023-10-04 19:30:00+00:00
Original Timestamp (Back to America/New_York): 2023-10-04 15:30:00-04:00
Original DatetimeIndex: DatetimeIndex(['2023-10-04 00:00:00+08:00', '2023-10-11 00:00:00+08:00',
'2023-10-18 00:00:00+08:00'],
dtype='datetime64[ns, Asia/Shanghai]', freq=None)
UTC DatetimeIndex: DatetimeIndex(['2023-10-03 16:00:00+00:00', '2023-10-10 16:00:00+00:00',
'2023-10-17 16:00:00+00:00'],
dtype='datetime64[ns, UTC]', freq=None)
Original DatetimeIndex (Back to Asia/Shanghai): DatetimeIndex(['2023-10-04 00:00:00+08:00', '2023-10-11 00:00:00+08:00',
'2023-10-18 00:00:00+08:00'],
dtype='datetime64[ns, Asia/Shanghai]', freq=None)
Working with Date and Time in PandasPandas provide convenient methods to extract specific date and time components from Timestamp objects. These methods include: Step-1: Create a dates dataframe Python3
Output: DatetimeIndex(['2011-01-01 00:00:00', '2011-01-01 01:00:00',
'2011-01-01 02:00:00', '2011-01-01 03:00:00',
'2011-01-01 04:00:00', '2011-01-01 05:00:00',
'2011-01-01 06:00:00', '2011-01-01 07:00:00',
'2011-01-01 08:00:00', '2011-01-01 09:00:00'],
dtype='datetime64[ns]', freq='H')
Step-2: Create range of dates and show basic features Python3
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. Step-3: Divide a given date into features – pandas.Series.dt.year returns the year of the date time. Break date and time into separate features Python3
Output: date year month day hour minute
0 2011-01-01 00:00:00 2011 1 1 0 0
1 2011-01-01 01:00:00 2011 1 1 1 0
2 2011-01-01 02:00:00 2011 1 1 2 0
Step-4: To get the present time, use Timestamp.now() and then convert timestamp to datetime and directly access year, month or day. Python3
Output: Timestamp('2018-09-18 17:18:49.101496')
Python3
Output: datetime.datetime(2018, 9, 18, 17, 18, 49, 101496)
Step-5: Extracting specific components of datetime columne like date, time, day of the week for further analysis. Python3
Output: 2018
8
25
15
53
Exploring UFO Sightings Over TimeLet’s analyze this problem on a real dataset uforeports. Python3
Output: City Colors Reported Shape Reported State Time
0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00
1 Willingboro NaN OTHER NJ 6/30/1930 20:00
2 Holyoke NaN OVAL CO 2/15/1931 14:00
3 Abilene NaN DISK KS 6/1/1931 13:00
4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00
The code is used to convert a column of time values in a Pandas DataFrame into the datetime format. Python3
Output: City Colors Reported Shape Reported State \
0 Ithaca NaN TRIANGLE NY
1 Willingboro NaN OTHER NJ
2 Holyoke NaN OVAL CO
3 Abilene NaN DISK KS
4 New York Worlds Fair NaN LIGHT NY
Time
0 1930-06-01 22:00:00
1 1930-06-30 20:00:00
2 1931-02-15 14:00:00
3 1931-06-01 13:00:00
4 1933-04-18 19:00:00
The code is used to display the data types of each column in a Pandas DataFrame. Python3
Output: City object
Colors Reported object
Shape Reported object
State object
Time datetime64[ns]
dtype: object
The code is used to extract the hour details from a column of time data in a Pandas DataFrame. Python3
Output: 0 22
1 20
2 14
3 13
4 19
Name: Time, dtype: int64
The code is used to retrieve the names of the weekdays for a column of date and time data in a Pandas DataFrame. Python3
Output: 0 Sunday
1 Monday
2 Sunday
3 Monday
4 Tuesday
Name: Time, dtype: object
The code is used to retrieve the ordinal day of the year for each date in a column of date and time data in a Pandas DataFrame. Python3
Output: 0 152
1 181
2 46
3 152
4 108
Name: Time, dtype: int64
Creating visualization to explore the frequency of UFO sightings by hour of the day. Python3
Output:
|
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 8 |