如何填充Python时间序列中特定日期范围的nan值?

发布于 2025-01-15 22:11:38 字数 1031 浏览 0 评论 0原文

我正在处理一个时间序列,记录了 2013 年至 2021 年巴西城市市场上鱼类的价格,原始数据集有三列,一列包含最便宜的价格,另一列包含最昂贵的价格,最后是他们收集数据当天发现的平均价格。我对相应的列、日期和日期进行了三个子集,然后进行了一些解释性分析,我发现 2013 年和 2014 年的一些特定月份具有 nan 值。

dfmin.loc['2013-4-1':'2013-7-31']
    min
date    
2013-04-01 12:00:00 16.0
2013-04-02 12:00:00 16.0
2013-05-22 12:00:00 NaN
2013-05-23 12:00:00 NaN
2013-05-24 12:00:00 NaN
2013-05-27 12:00:00 NaN
2013-05-28 12:00:00 NaN
2013-05-29 12:00:00 NaN
2013-05-30 12:00:00 NaN
2013-05-31 12:00:00 NaN
2013-06-03 12:00:00 NaN
2013-06-04 12:00:00 NaN
2013-06-05 12:00:00 NaN
2013-06-06 12:00:00 NaN
2013-06-07 12:00:00 NaN
2013-06-10 12:00:00 NaN
2013-06-11 12:00:00 NaN
2013-06-12 12:00:00 NaN
2013-06-13 12:00:00 NaN
2013-06-14 12:00:00 NaN
2013-06-17 12:00:00 NaN
2013-06-18 12:00:00 NaN
2013-06-19 12:00:00 15.8
2013-06-20 12:00:00 15.8
2013-06-21 12:00:00 15.8
​```

I want to fill these NaN values from the month 05 with the average value from the medium price from the month 04 and the month 06, how can I make it?

I'm working with a time series that have the recorded the prices from a fish in the markets from a Brazilian city from 2013 to 2021, the original dataset has three columns, one with the cheapest values founded, another with the most expensive ones and finally other with the average price found in the day they collected the data. I've made three subsets to the corresponding column, the dates and indexated the date then doing some explanatory analysis I founded that some specific months from 2013 and 2014 are with nan values.

dfmin.loc['2013-4-1':'2013-7-31']
    min
date    
2013-04-01 12:00:00 16.0
2013-04-02 12:00:00 16.0
2013-05-22 12:00:00 NaN
2013-05-23 12:00:00 NaN
2013-05-24 12:00:00 NaN
2013-05-27 12:00:00 NaN
2013-05-28 12:00:00 NaN
2013-05-29 12:00:00 NaN
2013-05-30 12:00:00 NaN
2013-05-31 12:00:00 NaN
2013-06-03 12:00:00 NaN
2013-06-04 12:00:00 NaN
2013-06-05 12:00:00 NaN
2013-06-06 12:00:00 NaN
2013-06-07 12:00:00 NaN
2013-06-10 12:00:00 NaN
2013-06-11 12:00:00 NaN
2013-06-12 12:00:00 NaN
2013-06-13 12:00:00 NaN
2013-06-14 12:00:00 NaN
2013-06-17 12:00:00 NaN
2013-06-18 12:00:00 NaN
2013-06-19 12:00:00 15.8
2013-06-20 12:00:00 15.8
2013-06-21 12:00:00 15.8
​```

I want to fill these NaN values from the month 05 with the average value from the medium price from the month 04 and the month 06, how can I make it?

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

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

发布评论

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

评论(1

放我走吧 2025-01-22 22:11:38

IIUC,您可以使用简单索引:

# if needed, convert to datetime
#df.index = pd.to_datetime(df.index)

df.loc[df.index.month==5, 'min'] = df.loc[df.index.month.isin([4,6]), 'min'].mean()

或者如果第 5 个月有非 NaN:

mask = df.index.month==5
df.loc[mask, 'min'] = (df.loc[mask, 'min']
                         .fillna(df.loc[df.index.month.isin([4,6]), 'min'].mean())
                       )

输出:

                       min
date                      
2013-04-01 12:00:00  16.00
2013-04-02 12:00:00  16.00
2013-05-22 12:00:00  15.88
2013-05-23 12:00:00  15.88
2013-05-24 12:00:00  15.88
2013-05-27 12:00:00  15.88
2013-05-28 12:00:00  15.88
2013-05-29 12:00:00  15.88
2013-05-30 12:00:00  15.88
2013-05-31 12:00:00  15.88
2013-06-03 12:00:00    NaN
2013-06-04 12:00:00    NaN
2013-06-05 12:00:00    NaN
2013-06-06 12:00:00    NaN
2013-06-07 12:00:00    NaN
2013-06-10 12:00:00    NaN
2013-06-11 12:00:00    NaN
2013-06-12 12:00:00    NaN
2013-06-13 12:00:00    NaN
2013-06-14 12:00:00    NaN
2013-06-17 12:00:00    NaN
2013-06-18 12:00:00    NaN
2013-06-19 12:00:00  15.80
2013-06-20 12:00:00  15.80
2013-06-21 12:00:00  15.80

IIUC, you can use simple indexing:

# if needed, convert to datetime
#df.index = pd.to_datetime(df.index)

df.loc[df.index.month==5, 'min'] = df.loc[df.index.month.isin([4,6]), 'min'].mean()

or if you have non NaN for the 5th month:

mask = df.index.month==5
df.loc[mask, 'min'] = (df.loc[mask, 'min']
                         .fillna(df.loc[df.index.month.isin([4,6]), 'min'].mean())
                       )

output:

                       min
date                      
2013-04-01 12:00:00  16.00
2013-04-02 12:00:00  16.00
2013-05-22 12:00:00  15.88
2013-05-23 12:00:00  15.88
2013-05-24 12:00:00  15.88
2013-05-27 12:00:00  15.88
2013-05-28 12:00:00  15.88
2013-05-29 12:00:00  15.88
2013-05-30 12:00:00  15.88
2013-05-31 12:00:00  15.88
2013-06-03 12:00:00    NaN
2013-06-04 12:00:00    NaN
2013-06-05 12:00:00    NaN
2013-06-06 12:00:00    NaN
2013-06-07 12:00:00    NaN
2013-06-10 12:00:00    NaN
2013-06-11 12:00:00    NaN
2013-06-12 12:00:00    NaN
2013-06-13 12:00:00    NaN
2013-06-14 12:00:00    NaN
2013-06-17 12:00:00    NaN
2013-06-18 12:00:00    NaN
2013-06-19 12:00:00  15.80
2013-06-20 12:00:00  15.80
2013-06-21 12:00:00  15.80
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文