将丢失的日期添加到CSV文件的时间序列中

发布于 2025-01-21 11:50:24 字数 323 浏览 3 评论 0原文

我有一个每月的时间序列的变量“ GWL”,但有几个丢失的日期。

import pandas as pd

df = pd.read_csv(r'1218_29_0.csv')
df.head(5)

    date        GWL
0   15/01/2001  9.73
1   15/08/2001  10.55
2   15/11/2001  11.65
3   15/01/2002  9.72
4   15/04/2002  9.92

我试图遵循其他帖子,但没有一个以CSV格式处理数据库。

如何添加缺失的日期(月)并填写NAN的价值?

I have a monthly time series of a variable 'GWL' but with several missing dates.

import pandas as pd

df = pd.read_csv(r'1218_29_0.csv')
df.head(5)

    date        GWL
0   15/01/2001  9.73
1   15/08/2001  10.55
2   15/11/2001  11.65
3   15/01/2002  9.72
4   15/04/2002  9.92

I have tried to follow other posts but none of them deal with a database in CSV format.

How can I add the missing dates (months) and fill their value by Nan?

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

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

发布评论

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

评论(1

破晓 2025-01-28 11:50:24

我加载使用:

df = pd.read_csv(io.StringIO('''date        GWL
15/01/2001  9.73
15/08/2001  10.55
15/11/2001  11.65
15/01/2002  9.72
15/04/2002  9.92'''), sep='\s{2,}', engine='python', parse_dates=['date'])

您需要在代码中执行的操作是将parse_dates = ['date']参数传递给pd.read_csv。不要通过其他东西。我需要使用io.stringio,因为您不会以构造函数格式提供数据。

这产生:

        date    GWL
0 2001-01-15   9.73
1 2001-08-15  10.55
2 2001-11-15  11.65
3 2002-01-15   9.72
4 2002-04-15   9.92

构建以IDES为中心的每月日期范围:

months = df['date'] - pd.offsets.MonthBegin()
d_range = pd.date_range(months.min(), months.max(), freq='M')
d_range = d_range - pd.offsets.MonthBegin() + pd.offsets.Day(14)

REINDEX:

>>> df.set_index('date').reindex(d_range)
              GWL
2001-01-15   9.73
2001-02-15    NaN
2001-03-15    NaN
2001-04-15    NaN
2001-05-15    NaN
2001-06-15    NaN
2001-07-15    NaN
2001-08-15  10.55
2001-09-15    NaN
2001-10-15    NaN
2001-11-15  11.65
2001-12-15    NaN
2002-01-15   9.72
2002-02-15    NaN
2002-03-15    NaN

I load using:

df = pd.read_csv(io.StringIO('''date        GWL
15/01/2001  9.73
15/08/2001  10.55
15/11/2001  11.65
15/01/2002  9.72
15/04/2002  9.92'''), sep='\s{2,}', engine='python', parse_dates=['date'])

What you need to do in your code is just pass the parse_dates=['date'] parameter to your pd.read_csv. Don't pass the other stuff. I need to use io.StringIO because you won't provide your data in a constructor format.

This yields:

        date    GWL
0 2001-01-15   9.73
1 2001-08-15  10.55
2 2001-11-15  11.65
3 2002-01-15   9.72
4 2002-04-15   9.92

Construct an Ides-centred monthly date range:

months = df['date'] - pd.offsets.MonthBegin()
d_range = pd.date_range(months.min(), months.max(), freq='M')
d_range = d_range - pd.offsets.MonthBegin() + pd.offsets.Day(14)

Reindex:

>>> df.set_index('date').reindex(d_range)
              GWL
2001-01-15   9.73
2001-02-15    NaN
2001-03-15    NaN
2001-04-15    NaN
2001-05-15    NaN
2001-06-15    NaN
2001-07-15    NaN
2001-08-15  10.55
2001-09-15    NaN
2001-10-15    NaN
2001-11-15  11.65
2001-12-15    NaN
2002-01-15   9.72
2002-02-15    NaN
2002-03-15    NaN
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文