简体   繁体   中英

Change in date from string to datetime object when converting pandas dataframe to dictionary

I have the foll. dataframe:

                avi       fi_id       dates
2017-07-17  0.318844    zab_a_002  2017-07-17

When I convert it into a dictionary, I get this:

dict_avi = df.reset_index().to_dict('records')

[{'index': Timestamp('2017-07-17 00:00:00'), 'avi': 0.3188438263036763, 'fi_id': 'zab_a_002', 'dates': datetime.date(2017, 7, 17)}]

Why did the dates column become a datetime object? How can I retain it as a string?

Here are the dtypes:

avi        float64
fi_id     object
dates        object
dtype: object

You want to make just the datetime columns strings instead

First, make sure those columns are actually of dtype datetime

df['index'] = pd.to_datetime(df['index'])
df['dates'] = pd.to_datetime(df['dates'])

Since we went through this trouble, we could have simply turned them into strings right then

df['index'] = pd.to_datetime(df['index']).astype(str)
df['dates'] = pd.to_datetime(df['dates']).astype(str)

But this wouldn't generalize.

What I'll do instead is use select_dtypes to grab only datetime columns and convert them to strings. Then I'll update the dataframe and dump into a new dictionary. All without messing with the dataframe.

df.assign(
    **df.select_dtypes(['datetime']).astype(str).to_dict('list')
).to_dict('records')

[{'avi': 0.3188438263036763,
  'dates': '2017-07-17',
  'fi_id': 'zab_a_002',
  'index': '2017-07-17'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM