Pandas is a popular library for data manipulation and analysis in Python. It provides powerful tools for working with time series data, including functions for reading and manipulating date and time data.
To read date and time data in Pandas, you can use the read_csv() function to read a CSV file that contains date and time columns. Here's an example:
import pandas as pd# read a CSV file that contains date and time columnsdf = pd.read_csv('data.csv', parse_dates=['Date', 'Time'])# print the first few rows of the dataframeprint(df.head()) |
In this example, we use the read_csv() function to read a CSV file called data.csv. We use the parse_dates parameter to specify the columns that contain date and time data. Pandas will automatically parse these columns and convert them to datetime objects.
Once we have read the data into a Pandas dataframe, we can use various functions to manipulate the date and time data. For example, we can extract the year, month, and day of the week from the date column using the dt accessor:
# extract the year, month, and day of the week from the date columndf['Year'] = df['Date'].dt.yeardf['Month'] = df['Date'].dt.monthdf['DayOfWeek'] = df['Date'].dt.dayofweek# print the updated dataframeprint(df.head()) |
In this example, we use the dt accessor to extract the year, month, and day of the week from the Date column, and store the results in new columns called Year, Month, and DayOfWeek. We can then print the updated dataframe to see the results.
Pandas provides many other functions for working with date and time data, including functions for resampling, shifting, and aggregating time series data. You can find more information in the Pandas documentation: https://pandas.pydata.org/docs/user_guide/timeseries.html