I currently use pd.read_csv to read the dataframe. It does not detect both date and datetime columns, but instead checks it out as object. So, I use the below snippet to find date/datetime columns:
df = pd.read_csv(filename)
collist = []
for col in df.columns:
if df[col].dtype == 'object':
try:
df[col] = pd.to_datetime(df[col])
collist.append(col)
except ValueError:
pass
return collist
But my use case needs me to have date columns and datetime columns separately. Is there a way how we can filter out date and datetime columns separately?
import pandas as pd
df = pd.DataFrame({
'date1':['4/10/2021', '4/11/2021','4/12/2021'],
'date_time1': ['4/11/2021 13:23:45', '4/11/2021 13:23:45', '4/11/2021 13:23:45'],
'Name': ['Bob', 'Jane', 'Alice'],
'date_time2': ['4/12/2021 13:23:45', '4/13/2021 13:23:45', '4/14/2021 13:23:45']
})
So, date column list should give me [date1] and datetime column list should give me [date_time1,date_time2]