Pandas – Working with Date and Time in DataFrames
With specialized date functions and methods, you can analyze time series in Pandas, perform calculations based on timestamps, and handle various date formats.
Bernd Fischer
04. Nov 2024 · 4 min read
Prerequisites
Make sure Pandas is installed:
pip install pandas
You can now work with the date and time functions and methods in Pandas.
Creating DataFrames with Date and Time
A DataFrame can contain date and time columns, which can be defined when creating the DataFrame or added later.
Example: Creating a DataFrame with a Date Range
Use the function pd.date_range to generate a sequence of date values that can be used as an index or a column in the DataFrame.
import pandas as pd
# Datumsbereich von 2023-01-01 bis 2023-01-10 mit täglicher Frequenz
dates = pd.date_range(start="2023-01-01", end="2023-01-10", freq="D")
df = pd.DataFrame({"Datum": dates, "Werte": range(10)})
print(df)
Here, a date range from January 1 to January 10, 2023 is created and stored as the column Datum in the DataFrame. The column Werte contains some sample numbers.
Converting Date and Time Values
Dates often appear in different formats and need to be converted into a Pandas-compatible date format. The function pd.to_datetime converts a column or a value into the datetime format.
# Beispiel-Daten
data = {"Datum": ["2023-01-01", "2023-01-02", "2023-01-03"], "Werte": [10, 15, 20]}
df = pd.DataFrame(data)
# Umwandlung der Spalte "Datum" in das datetime-Format
df["Datum"] = pd.to_datetime(df["Datum"])
print(df.dtypes)
This conversion ensures that Pandas interprets the values as dates, enabling time series operations and calculations.
Working with Date Functions
Pandas offers various methods to manipulate date values in DataFrames and access specific parts of a date.
Extracting Year, Month, and Day
Using the attributes .dt.year, .dt.month, and .dt.day, you can access specific parts of a date.
# Jahr, Monat und Tag extrahieren
df["Jahr"] = df["Datum"].dt.year
df["Monat"] = df["Datum"].dt.month
df["Tag"] = df["Datum"].dt.day
print(df)
This method adds three new columns to the DataFrame containing the year, month, and day extracted from the Datum column.
Calculations with Date Differences
Pandas allows you to calculate date differences, which is useful for analyzing time intervals.
# Differenz zwischen aufeinanderfolgenden Daten berechnen
df["Differenz_in_Tagen"] = df["Datum"].diff().dt.days
print(df)
The method .diff() computes the difference between consecutive rows in the Datum column. Combined with .dt.days, the difference is shown in days.
Time Series with a Date Index
In time series analysis, it is common to use a date index. Pandas lets you set date values as the index, simplifying access to specific time periods.
# Datum als Index festlegen
df = df.set_index("Datum")
print(df)
Now, the Datum is the index of the DataFrame. You can use date queries and filters to display only certain time frames.
Filtering by Date
With a date index, you can filter for specific periods.
# Filtern nach einem bestimmten Zeitraum
df_filtered = df["2023-01-02":"2023-01-04"]
print(df_filtered)
This filter displays data only from January 2 to January 4, 2023. Pandas automatically recognizes the date range and filters accordingly.
Resampling Time Series
Resampling allows you to adjust data to a specific frequency, for example, generating weekly or monthly data from daily data. This helps in identifying aggregated trends over periods.
# Resampling auf wöchentliche Summe
df_weekly = df.resample("W").sum()
print(df_weekly)
The method .resample("W").sum() sums the data for each week and returns the aggregated values.
Common Resampling Frequencies
"D"for daily"W"for weekly"M"for monthly"Q"for quarterly"A"for yearly
Time Shifts
With Pandas, you can shift time series to compare data or analyze delayed effects.
# Werte um eine Zeile nach unten verschieben
df["Verschobene_Werte"] = df["Werte"].shift(1)
print(df)
Here, the column Werte is shifted down by one row, which is useful when analyzing delays in time series.
Working with Timestamps and Time Intervals
For more precise timestamps, you can use the datetime format including hours, minutes, and seconds. Time intervals are also supported in Pandas and can be used for more detailed analysis.
Example: Timestamps with Hours and Minutes
# Beispiel-Daten mit Zeitstempel
data = {"Zeit": ["2023-01-01 10:00", "2023-01-01 11:00", "2023-01-01 12:00"], "Werte": [100, 150, 120]}
df = pd.DataFrame(data)
# Konvertierung in datetime-Format
df["Zeit"] = pd.to_datetime(df["Zeit"])
print(df)