xxxxxxxxxx
df['date_column'] = pd.to_datetime(df['datetime_column']).dt.date
xxxxxxxxxx
import pandas as pd
# Assuming you have a data frame called df with a column named 'date' containing object or string data
df['date'] = pd.to_datetime(df['date'])
# If the date column contains any format other than the default format '%Y-%m-%d', specify the format using the 'format' parameter
# For example, if the date format is '%d-%m-%Y', you can use:
# df['date'] = pd.to_datetime(df['date'], format='%d-%m-%Y')
xxxxxxxxxx
# This is a way to convert a dataframe into time series
# A dataframe is turned into time series once we make the dataframe's index into datetime datatype
# If the "date_col" is not converted to datetime type
df.index = pd.to_datetime(df["date_col"], errors='coerce')
# If the "date_col" is already converted to datetime type
df.index = df["date_col"]
xxxxxxxxxx
import pandas as pd
#create DataFrame
df = pd.DataFrame({'stamps': pd.date_range(start='2020-01-01 12:00:00',
periods=6,
freq='H'),
'sales': [11, 14, 25, 31, 34, 35]})
#convert column of timestamps to datetimes
df.stamps = df.stamps.apply(lambda x: x.date())
#view DataFrame
df
stamps sales
0 2020-01-01 11
1 2020-01-01 14
2 2020-01-01 25
3 2020-01-01 31
4 2020-01-01 34
5 2020-01-01 35
xxxxxxxxxx
df = pd.DataFrame({'year': [2015, 2016],
'month': [2, 3],
'day': [4, 5]})
pd.to_datetime(df)
0 2015-02-04
1 2016-03-05
dtype: datetime64[ns]