【问题标题】:get the start date of the week for pandas DatetimeIndex?获取 pandas DatetimeIndex 一周的开始日期?
【发布时间】:2021-10-27 23:17:47
【问题描述】:
date_idx 

DatetimeIndex(['2021-05-14', '2021-05-17', '2021-05-18', '2021-05-19',
               '2021-05-20', '2021-05-21', '2021-05-24', '2021-05-25',
               '2021-05-26', '2021-05-27', '2021-05-28', '2021-05-31',
               '2021-06-01', '2021-06-02', '2021-06-03', '2021-06-04',
               '2021-06-07', '2021-06-08', '2021-06-09', '2021-06-10',
               '2021-06-11', '2021-06-15', '2021-06-16', '2021-06-17',
               '2021-06-18', '2021-06-21', '2021-06-22', '2021-06-23',
               '2021-06-24', '2021-06-25', '2021-06-28', '2021-06-29',
               '2021-06-30', '2021-07-01', '2021-07-02', '2021-07-05',
               '2021-07-06', '2021-07-07', '2021-07-08', '2021-07-09',
               '2021-07-12', '2021-07-13', '2021-07-14', '2021-07-15',
               '2021-07-16', '2021-07-19', '2021-07-20', '2021-07-21',
               '2021-07-22', '2021-07-23'],
              dtype='datetime64[ns]', name='date', freq=None)

目标

  • 获取一周的开始日期

试试

date_idx- date_idx.weekday * np.timedelta64(1, 'D')
  • 参考this post,但它会得到一周的第一个日期。这不是我想要的。例如,'2021-06-15' 将是 '2021-06-14',但它应该是 '2021-06-15',因为 '2021-06-14' 不在 date_idx 中。

【问题讨论】:

    标签: pandas


    【解决方案1】:

    IIUC 你可以先groupbyfreq="W" 然后transform

    date_idx.to_frame(False).groupby(pd.Grouper(freq="W", key="date"))["date"].transform("first")
    
    0    2021-05-14
    1    2021-05-17
    2    2021-05-17
    3    2021-05-17
    4    2021-05-17
    5    2021-05-17
    6    2021-05-24
    7    2021-05-24
    8    2021-05-24
    9    2021-05-24
    10   2021-05-24
    11   2021-05-31
    12   2021-05-31
    13   2021-05-31
    14   2021-05-31
    15   2021-05-31
    16   2021-06-07
    17   2021-06-07
    18   2021-06-07
    19   2021-06-07
    20   2021-06-07
    21   2021-06-15
    22   2021-06-15
    23   2021-06-15
    24   2021-06-15
    25   2021-06-21
    26   2021-06-21
    27   2021-06-21
    28   2021-06-21
    29   2021-06-21
    30   2021-06-28
    31   2021-06-28
    32   2021-06-28
    33   2021-06-28
    34   2021-06-28
    35   2021-07-05
    36   2021-07-05
    37   2021-07-05
    38   2021-07-05
    39   2021-07-05
    40   2021-07-12
    41   2021-07-12
    42   2021-07-12
    43   2021-07-12
    44   2021-07-12
    45   2021-07-19
    46   2021-07-19
    47   2021-07-19
    48   2021-07-19
    49   2021-07-19
    Name: date, dtype: datetime64[ns]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-18
      • 2022-06-29
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      相关资源
      最近更新 更多