我有一个数据框,其中包含日期时间格式的“Date_and_time”列。不幸的是,当到达午夜时(第 15235 行:2020-08-02 00:00:00.000000),日期不会相应更改。因此,到达午夜 (00:00:00.000000) 时,2020-08-02 00:00:00.000000 应该转到 2020-08-03 00:00:00.00000。在第 15235 行已到达午夜,但日期直到第 16000 行才更改。如何更改此设置,以便在到达午夜时日期是正确的?谢谢。
#Load file
df = pd.read_csv(file, sep=";", names=["Date", "Time", "ID1","ID2","ID3","MP","ET"], skiprows = 1 ,float_precision='round_trip')
df1 = df['Time'].str.split(expand=True)
#Use columns 'Date' and 'Time' to create column 'Date_and_time' in datetime format
df['Date_and_time'] = (pd.to_datetime(df['Date']) +
pd.to_timedelta(df1[0]) +
pd.to_timedelta(df1[1].str.replace('ms','').astype(int), unit='us'))
Out[45]:
Date ... Date_and_time
0 2020/08/02 ... 2020-08-02 21:21:46.000000
1 2020/08/02 ... 2020-08-02 21:21:46.082191
2 2020/08/02 ... 2020-08-02 21:21:46.164383
3 2020/08/02 ... 2020-08-02 21:21:46.246575
4 2020/08/02 ... 2020-08-02 21:21:46.328767
... ... ...
15235 2020/08/02 ... 2020-08-02 00:00:00.000000
15236 2020/08/02 ... 2020-08-02 00:00:00.082191
15237 2020/08/02 ... 2020-08-02 00:00:00.164383
15238 2020/08/02 ... 2020-08-02 00:00:00.246575
... ... ...
16000 2020/08/03 ... 2020-08-03 00:00:16.082191
... ... ...
330404 2020/08/03 ... 2020-08-03 03:00:33.000000
330405 2020/08/03 ... 2020-08-03 03:00:33.040513
330406 2020/08/03 ... 2020-08-03 03:00:33.081026
330407 2020/08/03 ... 2020-08-03 03:00:33.121539
330408 2020/08/03 ... 2020-08-03 03:00:33.162052
[330409 rows x 8 columns]
倚天杖
相关分类