DateTime in Pandas: An Uncomplicated Guide (2023)
uncomplicated guide to datetime in Pandas for 2023:
- Timestamps
A Timestamp is a Pandas object that represents a single point in time. It can be created from a variety of data types, including strings, numbers, and dates.
For example, the following code creates a Timestamp from a string:
import pandas as pd
ts = pd.Timestamp('2023-06-04 10:44:43')
print(ts)
This will print the following output:
2023-06-04 10:44:43
- Periods
A Period is a Pandas object that represents a time period. It can be created from a variety of data types, including strings, numbers, and dates.
For example, the following code creates a Period from a string:
import pandas as pd
p = pd.Period('2023-06-04', freq='D')
print(p)
This will print the following output:
2023-06-04
- TimeSeries DataFrames
A TimeSeries DataFrame is a Pandas DataFrame that has a datetime index. This makes it easy to work with time-series data.
For example, the following code creates a TimeSeries DataFrame from a list of timestamps:
import pandas as pd
ts_list = [pd.Timestamp('2023-06-04 10:44:43'), pd.Timestamp('2023-06-05 10:44:43'), pd.Timestamp('2023-06-06 10:44:43')]
df = pd.DataFrame({'value': [1, 2, 3]}, index=ts_list)
print(df)
This will print the following output:
value
2023-06-04 1
2023-06-05 2
2023-06-06 3
- Slicing Time-Series
You can slice a TimeSeries DataFrame using the same syntax as you would use to slice a regular DataFrame. For example, the following code slices the DataFrame created above to only include the values for the first two days:
df = df.loc['2023-06-04':'2023-06-05']
print(df)
This will print the following output:
value
2023-06-04 1
2023-06-05 2
- DateTimeIndex Object and its Methods
The DateTimeIndex object is the underlying object that represents the index of a TimeSeries DataFrame. It has a number of methods that can be used to work with the index, such as:
-
year()
: Returns the year of the index. -
month()
: Returns the month of the index. -
day()
: Returns the day of the month of the index. -
hour()
: Returns the hour of the day of the index. -
minute()
: Returns the minute of the hour of the index. -
second()
: Returns the second of the minute of the index. -
Resampling Time-Series Data
Resampling is the process of aggregating or summarizing time-series data at a different frequency. For example, you could resample a daily time-series to weekly or monthly frequencies.
Pandas provides a number of methods for resampling time-series data, such as:
resample()
: This method allows you to specify the frequency of the resampling.mean()
: This method will calculate the mean of the values at the specified frequency.sum()
: This method will calculate the sum of the values at the specified frequency.max()
: This method will calculate the maximum value of the values at the specified frequency.min()
: This method will calculate the minimum value of the values at the specified frequency.
I hope this guide has been helpful. For more information on datetime in Pandas, please see