Pandas Timeseries 重新索引产生 NaN

发布于 2025-01-20 23:30:13 字数 1140 浏览 5 评论 0原文

当原始数据框架确实具有数值初始化时,我的reindex在整个数据帧中生产NAN感到惊讶。不知道为什么?

代码:

df = 
                               A  ...                            D
Unnamed: 0                        ...                                
2022-04-04 11:00:05          NaN  ...                          2419.0
2022-04-04 11:00:10          NaN  ...                          2419.0

## exp start and end times
exp_start, exp_end = '2022-04-04 11:00:00','2022-04-04 13:00:00'
## one second index
onesec_idx = pd.date_range(start=exp_start,end=exp_end,freq='1s')
## map new index to the df
df = df.reindex(onesec_idx)

结果:

df = 
                               A  ...                            D
2022-04-04 11:00:00          NaN  ...                             NaN
2022-04-04 11:00:01          NaN  ...                             NaN
2022-04-04 11:00:02          NaN  ...                             NaN
2022-04-04 11:00:03          NaN  ...                             NaN
2022-04-04 11:00:04          NaN  ...                             NaN
2022-04-04 11:00:05          NaN  ...                             NaN

I am surprised that my reindex is producing NaNs in whole dataframe when the original dataframe does have numerical values init. Don't know why?

Code:

df = 
                               A  ...                            D
Unnamed: 0                        ...                                
2022-04-04 11:00:05          NaN  ...                          2419.0
2022-04-04 11:00:10          NaN  ...                          2419.0

## exp start and end times
exp_start, exp_end = '2022-04-04 11:00:00','2022-04-04 13:00:00'
## one second index
onesec_idx = pd.date_range(start=exp_start,end=exp_end,freq='1s')
## map new index to the df
df = df.reindex(onesec_idx)

Result:

df = 
                               A  ...                            D
2022-04-04 11:00:00          NaN  ...                             NaN
2022-04-04 11:00:01          NaN  ...                             NaN
2022-04-04 11:00:02          NaN  ...                             NaN
2022-04-04 11:00:03          NaN  ...                             NaN
2022-04-04 11:00:04          NaN  ...                             NaN
2022-04-04 11:00:05          NaN  ...                             NaN

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走走停停 2025-01-27 23:30:13

从文档中您可以看到 df.reindex() 将将 NA/NaN 放置在先前索引中没有值的位置。

但是您也可以提供一个值来替换缺失值(它默认为 NaN):

df.reindex(onesec_idx, fill_value='')

如果您想替换特定列中甚至整个数据框中的 NaN,您可以在执行重新索引后运行类似的操作:

df.fillna('',inplace=True)  # for replacing NaN in the entire df with ''

df['d'].fillna(0, inplace=True)  # if you want to replace all NaN in the D column with 0

来源:

重新索引文档:https://pandas.pydata.org/pandas- docs/stable/reference/api/pandas.DataFrame.reindex.html

fillna 的文档:https://pandas.pydata.org/pandas-文档/stable/reference/api/pandas.DataFrame.fillna.html

From the documentation you can see that df.reindex() will Places NA/NaN in locations having no value in the previous index.

However you can also provide a value that you want to replace missing values with (It defaults to NaN):

df.reindex(onesec_idx, fill_value='')

If you want to replace the NaN in a particular column or even in the whole dataframe you can run something like after doing a reindex:

df.fillna('',inplace=True)  # for replacing NaN in the entire df with ''

df['d'].fillna(0, inplace=True)  # if you want to replace all NaN in the D column with 0

Sources:

Documentation for reindex: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reindex.html

Documentation for fillna: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.fillna.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文