SoFunction
Updated on 2024-11-18

pandas performs the conversion of time data and calculates the time difference and extracts the year, month and day.

#pd.to_datetime function

#Read the data
import pandas as pd
data = pd.read_csv('')
 
# Convert stop_date to datetime format dataframe, save to stop_datetime
data['stop_datetime'] = pd.to_datetime(data.stop_date')

# Customize a time and calculate the time difference

data_new = pd.to_datetime('2006-01-01')
data['time_d'] = time_new - data.stop_datetime
data['time_d'].head()

# of occurrences by year and month counted

data.stop_datetime..value_counts()
data.stop_datetime..value_counts()

# Withdrawal year, month, day

# of years withdrawn
data['year'] = data.stop_datetime.
data['year'].head()
 
# Month of extraction
data['month'] = data.stop_datetime.
data['month'].head()
 
# Withdrawal date
data['day'] = data.stop_datetime.
data['day'].head()

# Plotting with time series data

data['stop_time_datetime'] = pd.to_datetime(data.stop_time)
(data.stop_time_datetime.).drugs_related_stop.sum().plot()

This is the whole content of this article.