gpt4 book ai didi

python - :doy:sod format 年的 Pandas date_parser 函数

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:44 26 4
gpt4 key购买 nike

我有一个表格,其中日期和时间的格式为 year:day_of_year:seconds_of_day(第 2 列)。可以找到示例文件 here

ABCD 15:010:00000 2564.6   4.0  -0.380  0.417  -1.313  0.520
ABCD 15:010:00300 2564.3 3.7 -0.389 0.396 -1.318 0.503
ABCD 15:010:00600 2563.9 3.5 -0.397 0.389 -1.324 0.496
ABCD 15:010:00900 2563.9 3.3 -0.411 0.368 -1.322 0.476
ABCD 15:010:01200 2563.8 3.0 -0.425 0.361 -1.320 0.466
ABCD 15:010:01500 2563.9 2.8 -0.432 0.340 -1.312 0.447
ABCD 15:010:01800 2564.3 2.6 -0.439 0.334 -1.304 0.439

我使用 pandas 包将上面的表格放在 pandas.DataFrame 中:

names=['Site', 'Epoch', 'TroTot', 'Stdev','TgnTot', 'TgnStd', 'TgeTot', 'TgeStd']
parser = lambda x: pd.datetime.strptime(x, '%y:%j:%???')
df = pd.read_csv([FILE][1],
header=None,
names=names,
delim_whitespace=True,
parse_dates=['Epoch'],
date_parser=parser)

我查看了文档,但在我看来,一天中的秒数没有格式。我怎样才能更改解析器函数以正确处理这种格式?

最佳答案

您可以先按原样读取您的 CSV 文件(不解析日期):

In [207]: df = pd.read_csv(fn, delim_whitespace=True, header=None, names=names)

In [208]: df
Out[208]:
Site Epoch TroTot Stdev TgnTot TgnStd TgeTot TgeStd
0 ABCD 15:010:00000 2564.6 4.0 -0.380 0.417 -1.313 0.520
1 ABCD 15:010:00300 2564.3 3.7 -0.389 0.396 -1.318 0.503
2 ABCD 15:010:00600 2563.9 3.5 -0.397 0.389 -1.324 0.496
3 ABCD 15:010:00900 2563.9 3.3 -0.411 0.368 -1.322 0.476
4 ABCD 15:010:01200 2563.8 3.0 -0.425 0.361 -1.320 0.466
5 ABCD 15:010:01500 2563.9 2.8 -0.432 0.340 -1.312 0.447
6 ABCD 15:010:01800 2564.3 2.6 -0.439 0.334 -1.304 0.439
7 ABCD 15:010:02100 2564.5 2.5 -0.453 0.314 -1.302 0.423
8 ABCD 15:010:02400 2564.2 2.4 -0.467 0.309 -1.299 0.419
9 ABCD 15:010:02700 2563.7 2.3 -0.482 0.287 -1.305 0.404
.. ... ... ... ... ... ... ... ...
278 ABCD 15:010:83400 2561.6 2.2 0.033 0.276 -0.894 0.416
279 ABCD 15:010:83700 2562.1 2.2 0.053 0.271 -0.897 0.418
280 ABCD 15:010:84000 2562.7 2.3 0.073 0.285 -0.899 0.431
281 ABCD 15:010:84300 2562.6 2.3 0.108 0.283 -0.869 0.431
282 ABCD 15:010:84600 2562.7 2.3 0.144 0.299 -0.839 0.442
283 ABCD 15:010:84900 2562.4 2.3 0.175 0.298 -0.824 0.441
284 ABCD 15:010:85200 2562.4 2.3 0.207 0.313 -0.810 0.450
285 ABCD 15:010:85500 2562.1 2.3 0.228 0.314 -0.805 0.453
286 ABCD 15:010:85800 2562.2 2.5 0.249 0.331 -0.801 0.467
287 ABCD 15:010:86100 2562.6 2.7 0.253 0.337 -0.796 0.473

[288 rows x 8 columns]

现在您可以按如下方式解析 Epoch:

In [209]: df['Epoch'] = pd.to_datetime(df['Epoch'].str[:6], format='%y:%j') + \
...: pd.to_timedelta(df['Epoch'].str[7:].astype(int), unit='s')
...:

In [210]: df
Out[210]:
Site Epoch TroTot Stdev TgnTot TgnStd TgeTot TgeStd
0 ABCD 2015-01-10 00:00:00 2564.6 4.0 -0.380 0.417 -1.313 0.520
1 ABCD 2015-01-10 00:05:00 2564.3 3.7 -0.389 0.396 -1.318 0.503
2 ABCD 2015-01-10 00:10:00 2563.9 3.5 -0.397 0.389 -1.324 0.496
3 ABCD 2015-01-10 00:15:00 2563.9 3.3 -0.411 0.368 -1.322 0.476
4 ABCD 2015-01-10 00:20:00 2563.8 3.0 -0.425 0.361 -1.320 0.466
5 ABCD 2015-01-10 00:25:00 2563.9 2.8 -0.432 0.340 -1.312 0.447
6 ABCD 2015-01-10 00:30:00 2564.3 2.6 -0.439 0.334 -1.304 0.439
7 ABCD 2015-01-10 00:35:00 2564.5 2.5 -0.453 0.314 -1.302 0.423
8 ABCD 2015-01-10 00:40:00 2564.2 2.4 -0.467 0.309 -1.299 0.419
9 ABCD 2015-01-10 00:45:00 2563.7 2.3 -0.482 0.287 -1.305 0.404
.. ... ... ... ... ... ... ... ...
278 ABCD 2015-01-10 23:10:00 2561.6 2.2 0.033 0.276 -0.894 0.416
279 ABCD 2015-01-10 23:15:00 2562.1 2.2 0.053 0.271 -0.897 0.418
280 ABCD 2015-01-10 23:20:00 2562.7 2.3 0.073 0.285 -0.899 0.431
281 ABCD 2015-01-10 23:25:00 2562.6 2.3 0.108 0.283 -0.869 0.431
282 ABCD 2015-01-10 23:30:00 2562.7 2.3 0.144 0.299 -0.839 0.442
283 ABCD 2015-01-10 23:35:00 2562.4 2.3 0.175 0.298 -0.824 0.441
284 ABCD 2015-01-10 23:40:00 2562.4 2.3 0.207 0.313 -0.810 0.450
285 ABCD 2015-01-10 23:45:00 2562.1 2.3 0.228 0.314 -0.805 0.453
286 ABCD 2015-01-10 23:50:00 2562.2 2.5 0.249 0.331 -0.801 0.467
287 ABCD 2015-01-10 23:55:00 2562.6 2.7 0.253 0.337 -0.796 0.473

[288 rows x 8 columns]

检查:

In [211]: df.dtypes
Out[211]:
Site object
Epoch datetime64[ns]
TroTot float64
Stdev float64
TgnTot float64
TgnStd float64
TgeTot float64
TgeStd float64
dtype: object

关于python - :doy:sod format 年的 Pandas date_parser 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42736986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com