I am trying to read data from a csv file & i'm performing the following transformations
- Read the data
- convert time column to
datetime[ns] - Filter the dataframe to only include rows where the hour of the
timecolumn is8 - Format the
timecolumn to only include the date part
In both cases the code works but in the first one, timecolumn becomes datetime[ns] dtype and in the second one it remains as object
I want to use the second code (since it solves both problems) and convert the dtype to object.
Following are both the codes,
df_2020 = pd.read_csv("2020.csv")
df[column_name] = pd.to_datetime(df[column_name], format=date_format)
return df```
`df_2020 = convert_to_datetime(df_2020, 'time', 'ISO8601')`
```def filter_and_format(df):
df = df[df['time'].dt.hour == 8]
df['time'] = df['time'].dt.date
return df```
`df_2020 = filter_and_format(df_2020)`
And the second code
```def convert_filter_format(df, column_name, date_format):
df[column_name] = pd.to_datetime(df[column_name], format=date_format)
df = df[df[column_name].dt.hour == 8]
df[column_name] = df[column_name].dt.date
return df```
`df_2020 = convert_filter_format(df_2020,'time','ISO8601')`
I want to use the second code. It should convert time column to `datetime[ns]` format