我有一个dtype datetime64 [ns]的熊猫系列。看起来像这样:
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
我想设置自己的原始日期(datetime的时间部分将保持不变),因此看起来像这样:
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
完成此任务的最佳方法是什么?
答案 0 :(得分:0)
假设您的列名为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]
答案 1 :(得分:0)
如果您想要一个任意原点,则可以将其与该日期时间的差求和:
df['Date'] = df['Date'].apply(lambda x: x + datetime.datetime(2019, 2, 19) - datetime.datetime(1970, 1, 1))