【问题标题】:Select date period in calendar year for several years by Pandas熊猫在日历年中选择几年的日期时间段
【发布时间】:2021-05-14 21:26:37
【问题描述】:

我想在日历中选择几年的日期。所以我们假设每年的 3 月 25 日到 8 月 25 日。我正在测试这种方法:Filtering data for multiple years by date range given by months and days in pandas dataframe,但它对我不起作用。日期的格式是 datetime64[ns],所以我不太确定出了什么问题。我正在测试不同的方法,最后总是只选择月份,没有日期限制。

这是我的代码 sn-p:

np.random.seed(0)
data = pd.DataFrame({'
    date': pd.date_range('1990-01-01', freq='d', periods=10000),
    'yield': np.random.randn(10000).cumsum(),
    'simulation': np.random.randn(10000).cumsum(),
    'Predicted': np.random.randn(10000).cumsum()})
out:
        date           yield    simulation  Predicted
0       1990-01-01     1.764     -0.202     0.330
1       1990-01-02     2.164     -1.035     0.330
2       1990-01-03     3.143     0.698      1.148
3       1990-01-04     5.384     0.889      1.576
4       1990-01-05     7.251     0.711     -0.928
...............................................
9995    2017-05-14  -186.162   111.432    -56.764
9996    2017-05-15  -186.119   111.323    -57.349
9997    2017-05-16  -185.602   111.266    -58.861
9998    2017-05-17  -185.635   110.207    -57.884
9999    2017-05-18  -184.337   109.880    -56.628

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    尝试为每个月的日期创建一个布尔索引,然后or将它们放在一起

    import numpy as np
    import pandas as pd
    
    np.random.seed(0)
    data = pd.DataFrame({
        'date': pd.date_range('1990-01-01', freq='d', periods=10000),
        'yield': np.random.randn(10000).cumsum(),
        'simulation': np.random.randn(10000).cumsum(),
        'Predicted': np.random.randn(10000).cumsum()
    })
    
    march_dates = (
            data['date'].dt.month.eq(3) &
            data['date'].dt.day.between(25, 31)
    )
    april_dates = (
            data['date'].dt.month.eq(4) &
            data['date'].dt.day.between(1, 25)
    )
    m = march_dates | april_dates
    
    filtered_df = data[m]
    print(filtered_df)
    

    filtered_df:

               date       yield  simulation  Predicted
    83   1990-03-25   -3.778287   11.467327   5.179931
    84   1990-03-26   -2.290035    9.555657   7.388960
    85   1990-03-27   -0.394146   10.490462   8.468163
    86   1990-03-28    0.784634   11.336408   9.594991
    87   1990-03-29    0.604709   11.454873   9.741437
    ...         ...         ...         ...        ...
    9972 2017-04-21 -185.894256  103.212971 -62.513663
    9973 2017-04-22 -186.199773  101.885143 -63.431743
    9974 2017-04-23 -186.804921  101.908402 -63.195393
    9975 2017-04-24 -186.580100  100.244993 -62.907841
    9976 2017-04-25 -187.618573  100.814717 -62.071467
    
    [896 rows x 4 columns]
    

    【讨论】:

      【解决方案2】:

      很遗憾,您没有提供可重现的示例。

      1. 您应该将日期列设为 DataFrame 索引,因为这是 DataFrame 的全部理念。
      2. 然后您可以轻松地将 .month() 和 .day() 方法应用于索引并根据您的需要对其进行过滤。
      3. 如果您不熟悉 DataFrame 索引,请先阅读文档
        例如https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html

      【讨论】:

        【解决方案3】:

        您可以使用查询功能:

        data.query("date >= '1990-03-25' and date <= '1991-08-25'")
        

        输出:

                  date     yield  simulation  Predicted
        83  1990-03-25  3.871510    1.124867  -0.641264
        84  1990-03-26  3.463583    1.308551  -1.727582
        85  1990-03-27  2.730999    0.265739  -2.836030
        86  1990-03-28  2.689405   -0.602710  -4.748007
        87  1990-03-29  2.678018   -3.826888  -4.159625
        ..         ...       ...         ...        ...
        597 1991-08-21 -1.645160    6.778537  -6.120725
        598 1991-08-22 -2.304405    6.092513  -6.671160
        599 1991-08-23 -3.955274    5.144826  -6.719812
        600 1991-08-24 -3.554355    4.038627  -6.316459
        601 1991-08-25 -4.692877    3.711540  -6.723403
        

        【讨论】:

        • 是的,这只是一年的解决方案 :-)
        • 1990 是问题所在。 1991 会发生什么?
        • @toptalent 测试,一年解决方案。
        猜你喜欢
        • 2016-07-27
        • 1970-01-01
        • 2018-04-21
        • 2015-09-19
        • 2019-02-05
        • 2022-11-16
        • 2016-10-30
        • 1970-01-01
        相关资源
        最近更新 更多