【问题标题】:How would I change the origin date of a pandas series that is of dtype datetime64[ns]?如何更改 dtype datetime64[ns] 的 pandas 系列的原始日期?
【发布时间】:2025-12-09 03:25:01
【问题描述】:

我有一个数据类型为 datetime64[ns] 的 pandas 系列。这是它的样子:

0      1970-01-01 00:00:00
1      1970-01-01 00:00:01
2      1970-01-01 00:00:02
3      1970-01-01 00:00:03
4      1970-01-01 00:00:04
5      1970-01-01 00:00:05
6      1970-01-01 00:00:06
7      1970-01-01 00:00:07
8      1970-01-01 00:00:08
9      1970-01-01 00:00:09
10     1970-01-01 00:00:10
11     1970-01-01 00:00:11
12     1970-01-01 00:00:12
13     1970-01-01 00:00:13
14     1970-01-01 00:00:14
15     1970-01-01 00:00:15
16     1970-01-01 00:00:16

我想设置我自己的原始日期(日期时间的时间部分将保持不变),所以它看起来像这样:

0      2019-02-19 00:00:00
1      2019-02-19 00:00:01
2      2019-02-19 00:00:02
3      2019-02-19 00:00:03
4      2019-02-19 00:00:04
5      2019-02-19 00:00:05
6      2019-02-19 00:00:06
7      2019-02-19 00:00:07
8      2019-02-19 00:00:08
9      2019-02-19 00:00:09
10     2019-02-19 00:00:10
11     2019-02-19 00:00:11
12     2019-02-19 00:00:12
13     2019-02-19 00:00:13
14     2019-02-19 00:00:14
15     2019-02-19 00:00:15
16     2019-02-19 00:00:16

最好的方法是什么?

【问题讨论】:

    标签: python pandas datetime series


    【解决方案1】:

    假设您的列名为Date,请使用Timestamp.replace

    df['Date'].apply(lambda dt: dt.replace(2019, 2, 19))
    # or more self expalantory
    # df['Date'].apply(lambda dt: dt.replace(year=2019, month=2, day=19))
    
    0    2019-02-19 00:00:00
    1    2019-02-19 00:00:01
    2    2019-02-19 00:00:02
    3    2019-02-19 00:00:03
    4    2019-02-19 00:00:04
    5    2019-02-19 00:00:05
    6    2019-02-19 00:00:06
    7    2019-02-19 00:00:07
    8    2019-02-19 00:00:08
    9    2019-02-19 00:00:09
    10   2019-02-19 00:00:10
    11   2019-02-19 00:00:11
    12   2019-02-19 00:00:12
    13   2019-02-19 00:00:13
    14   2019-02-19 00:00:14
    15   2019-02-19 00:00:15
    16   2019-02-19 00:00:16
    Name: Date, dtype: datetime64[ns]
    

    【讨论】:

      【解决方案2】:

      如果你想要一个任意的来源,你可以将差值与那个日期时间相加:

      df['Date'] = df['Date'].apply(lambda x: x + datetime.datetime(2019, 2, 19) - datetime.datetime(1970, 1, 1))
      

      【讨论】: