简体   繁体   English

将 Pandas 数据帧中的所有 NaT 值更改为 Timedelta 00:00:00

[英]Change all NaT values in Pandas dataframe to Timedelta 00:00:00

I have a dataframe in Pandas, and one column "timeOff" has some NaT values.我在 Pandas 中有一个数据框,其中一列“timeOff”有一些 NaT 值。

All I want to do is change all the NaT values to timeDelta values with '00:00:00' as the value.我想要做的就是将所有 NaT 值更改为 timeDelta 值,并以 '00:00:00' 作为值。

This is my current output:这是我当前的输出:

Output with NaT values带有 NaT 值的输出

I have tried to run this line of code:我试图运行这行代码:

replaceNaT = pd.to_timedelta('00:00:00')
print(replaceNaT)

startEndEventsDataframe['timeOff'] = np.where(pd.isnull(startEndEventsDataframe['timeOff']) == True, replaceNaT, startEndEventsDataframe['timeOff'])

But this destroys all the values in my dataframe column, as seen below:但这会破坏我的数据框列中的所有值,如下所示:

After running code from above从上面运行代码后

I would like for all the values that are not NaT to remain unchanged, and I would like all values that are NaT to be timeDelta with values "00:00:00".我希望所有不是 NaT 的值保持不变,并且我希望所有 NaT 值都是 timeDelta,其值为“00:00:00”。

Thanks for the help.谢谢您的帮助。

So, as it turns out I figured it out on my own, but figured I would post the solution to anybody who might need to know in the future.所以,事实证明,我是自己想出来的,但我想我会把解决方案发布给将来可能需要知道的任何人。

I got rid of the "replaceNaT" and simply wrote "0" in where NaT was found.我去掉了“replaceNaT”,只是在找到 NaT 的地方写了“0”。 I guess timeDeltas are stored as integers based on the lowest resolution of time they measure, and are only converted to look like they do when they are displayed?我猜 timeDeltas 根据它们测量的最低时间分辨率存储为整数,并且只在显示时转换为它们的样子?

Anyways, here is the code change that worked for me:无论如何,这是对我有用的代码更改:

startEndEventsDataframe['timeOff'] = np.where(pd.isnull(startEndEventsDataframe['timeOff']) == True, 0, startEndEventsDataframe['timeOff'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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