Pandas Tutorial
Creating Objects
Viewing Data
Selection
Manipulating Data
Grouping Data
Merging, Joining and Concatenating
Working with Date and Time
Working With Text Data
Working with CSV and Excel files
Operations
Visualization
Applications and Projects
Working with dates and times is a common operation in data analysis, and pandas provides robust tools for datetime manipulations. In this tutorial, we'll go over how to obtain datetime objects using pandas.
Ensure you have pandas installed:
pip install pandas
import pandas as pd
If you have a date or datetime represented as a string, you can convert it into a pandas datetime object using the pd.to_datetime()
function.
date_string = "2022-08-31" date_object = pd.to_datetime(date_string) print(date_object)
You can obtain the current date and time using pd.Timestamp.now()
:
current_datetime = pd.Timestamp.now() print(current_datetime)
If your date string has a unique format, you can specify it using the format
parameter:
date_string_unique_format = "31-08-2022" date_object_unique_format = pd.to_datetime(date_string_unique_format, format='%d-%m-%Y') print(date_object_unique_format)
You can create a sequence of dates using pd.date_range()
:
date_range = pd.date_range(start='2022-01-01', end='2022-01-10', freq='D') print(date_range)
This will create a datetime index with dates from January 1, 2022, to January 10, 2022.
Pandas can also handle time zone aware datetime objects:
datetime_with_timezone = pd.Timestamp.now(tz='UTC') print(datetime_with_timezone)
To convert between time zones, you can use the tz_convert
method:
datetime_in_paris = datetime_with_timezone.tz_convert('Europe/Paris') print(datetime_in_paris)
Once you have a datetime object, you can extract various components:
# Getting the year, month, and day print("Year:", current_datetime.year) print("Month:", current_datetime.month) print("Day:", current_datetime.day)
Pandas provides a rich set of functionalities for handling dates and times. Whether you're converting strings to datetime objects, creating date ranges, or working with time zones, pandas has the tools to make the process efficient and intuitive. It's important to familiarize yourself with these tools if you're planning to work with temporal data in your analyses.
Convert string to datetime in Pandas:
pd.to_datetime()
function to convert a string to a datetime object in Pandas.import pandas as pd # Sample string date_string = '2022-01-01' # Convert string to datetime datetime_object = pd.to_datetime(date_string)
Creating datetime objects in Pandas:
pd.to_datetime()
function or pd.Timestamp
constructor to create datetime objects in Pandas.import pandas as pd # Create datetime using to_datetime datetime_object_1 = pd.to_datetime('2022-01-01') # Create datetime using Timestamp constructor datetime_object_2 = pd.Timestamp('2022-01-01')
Parsing dates in Pandas DataFrame:
pd.to_datetime()
function to parse dates in a Pandas DataFrame.import pandas as pd # Sample DataFrame with a date column df = pd.DataFrame({'date': ['2022-01-01', '2022-01-02', '2022-01-03']}) # Parse dates in the DataFrame df['date'] = pd.to_datetime(df['date'])
Extracting date and time components in Pandas:
.dt
accessor to extract date and time components from a Pandas datetime column.import pandas as pd # Sample DataFrame with a datetime column df = pd.DataFrame({'datetime': pd.to_datetime(['2022-01-01 08:00', '2022-01-02 12:30', '2022-01-03 18:45'])}) # Extract date and time components df['date'] = df['datetime'].dt.date df['hour'] = df['datetime'].dt.hour df['minute'] = df['datetime'].dt.minute
Pandas datetime indexing and selection:
import pandas as pd # Sample DataFrame with a datetime column df = pd.DataFrame({'datetime': pd.to_datetime(['2022-01-01', '2022-01-02', '2022-01-03']), 'value': [10, 20, 30]}) # Set datetime as the index df.set_index('datetime', inplace=True) # Select data based on a date range selected_data = df['2022-01-01':'2022-01-02']
Handling datetime formats in Pandas:
format
parameter in pd.to_datetime()
for parsing datetime with a specific format.import pandas as pd # Sample string with a specific format date_string = '01-01-2022 12:30' # Convert string to datetime with a format datetime_object = pd.to_datetime(date_string, format='%d-%m-%Y %H:%M')
Convert integer timestamp to datetime in Pandas:
pd.to_datetime()
function to convert integer timestamps to datetime objects in Pandas.import pandas as pd # Sample integer timestamp timestamp = 1641000000 # Example timestamp for 2022-01-01 00:00:00 # Convert timestamp to datetime datetime_object = pd.to_datetime(timestamp, unit='s')
Resampling time series data in Pandas:
.resample()
method to resample time series data to a different frequency.import pandas as pd # Sample DataFrame with a datetime index df = pd.DataFrame({'value': [10, 20, 30]}, index=pd.to_datetime(['2022-01-01', '2022-01-02', '2022-01-03'])) # Resample to daily frequency resampled_df = df.resample('D').sum()
Dealing with time zones in Pandas datetime:
.tz_localize()
and .tz_convert()
methods to handle time zones in Pandas datetime.import pandas as pd # Sample datetime with a time zone datetime_with_tz = pd.to_datetime('2022-01-01 12:00').tz_localize('UTC') # Convert time zone datetime_converted = datetime_with_tz.tz_convert('America/New_York')