pandas to_dict with python native datetime type and not timestamp

mos*_*evi 5 python timestamp python-datetime pandas

I have a pandas DataFrame df that contains Timesatamp columns.

I wish to create an iterator of rows (either via the iter.. methods or via to_dict) from df where the Timesatamp values are python datetime.

I have tried doing this

for col in df.select_dtypes(['datetime']):
        df[col] = df[col].dt.to_pydatetime()
Run Code Online (Sandbox Code Playgroud)

however it seems like the columns is still Timesatamp when using the above mentioned iterator methods. Is there a 'batch'y way to achieve this apart from manualy converting each values when its iterated upon?


example

df = pd.DataFrame({'d': pd.date_range('2018-01-01', freq='12h', periods=2), 'a':[1,2]})
for col in df.select_dtypes(['datetime']):
    df[col] = df[col].dt.to_pydatetime()
print(df.to_dict('records'))
Run Code Online (Sandbox Code Playgroud)

the output:

[{'d': Timestamp('2018-01-01 00:00:00'), 'a': 1}, {'d': Timestamp('2018-01-01 12:00:00'), 'a': 2}]
Run Code Online (Sandbox Code Playgroud)

the desired output:

[{'d': datetime.datetime(2018, 1, 1, 0, 0), 'a': 1}, {'d': datetime.datetime(2018, 1, 1, 12, 0), 'a': 2}]
Run Code Online (Sandbox Code Playgroud)

小智 2

你可以试试

df[col] = pd.Series(df[col].dt.to_pydatetime(), dtype = object)
Run Code Online (Sandbox Code Playgroud)

代替

df[col] = df[col].dt.to_pydatetime()
Run Code Online (Sandbox Code Playgroud)

  • 看起来这确实使列成为“object”类型,但是看起来“to_dict('records')”将其强制回“Timestamp”,而简单的“to_dict()”则不然。我已经在 github 上提交了一个[问题](https://github.com/pandas-dev/pandas/issues/29824) (2认同)