在datetime索引中,如何将所有日期更改为特定日期。或起源

2024-06-17 15:14:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个来自日志文件的CVS数据:

UTC_TIME,FOCUS,IRIS,ZOOM,PAN,TILT,ROLL,LONGITUDE,LATITUDE,ALTITUDE,RED_RECORD
23:2:24.1, 10.9, 32.0, 180.0, 16.7, -29.5, -0.0, 151.206135, -33.729484, 1614.3, 0
23:2:24.2, 10.9, 32.0, 180.0, 16.7, -29.5, -0.0, 151.206135, -33.729484, 1614.3, 0
23:2:24.3, 10.9, 32.0, 180.0, 16.7, -29.5, -0.0, 151.206135, -33.729484, 1614.3, 0

这是我目前的代码:

vfx_df = pd.read_csv(data, header=0, low_memory=False)

我必须把“纳秒”分开,因为它们是fps而不是纳秒

vfx_df['UTC_TIME'] = vfx_df['UTC_TIME'].str.split('.', n = 1, expand=True)
vfx_df['UTC_TIME'] = pd.to_datetime(vfx_df['UTC_TIME'], format='%H:%M:%S')
vfx_df.set_index('UTC_TIME', inplace=True, drop=True)
vfx_df = vfx_df.tz_localize('UTC')
vfx_df = vfx_df.tz_convert('Australia/Sydney')

我留下这些结果:1900-01-02 09:32:20+10:05 如何将年、日、月更改为实际拍摄的日期。 还考虑拍摄的过程可以超过6小时,所以日志中的UTC时间戳可以在本地时间第二天到达?p>

我已尝试在导入时设置原点,并且:

 vfx_df['UTC_TIME'] = pd.to_datetime(vfx_df['UTC_TIME'], format='%H:%M:%S' unit='D' origin=(pd.Timestamp('2020-03-03')))

我已经研究了时间增量和偏移量,但我似乎无法得到它。。。 我只是觉得我做错了什么,我只是想看到一个更像Python的方式来做这件事

谢谢


Tags: 文件to数据trueformatdfdatetimetime
1条回答
网友
1楼 · 发布于 2024-06-17 15:14:15

不确定日期来自何处,但如果您试图手动输入,可以将其字符串格式设置为时间戳:

import pandas as pd
from datetime import datetime

def parseDT(input_dt, ts):
    out = datetime.strptime(f'{input_dt} {ts}', '%Y-%m-%d %H:%M:%S.%f')
    return(out)

input_dt = '2020-04-20'
vfx_df['UTC_DATETIME'] = [parseDT(input_dt, ts) for ts in vfx_df['UTC_TIME']]

这将产生:

In [34]: vfx_df
Out[34]:
    UTC_TIME  FOCUS  IRIS   ZOOM   PAN  TILT  ROLL   LONGITUDE   LATITUDE  ALTITUDE  RED_RECORD            UTC_DATETIME
0  23:2:24.1   10.9  32.0  180.0  16.7 -29.5  -0.0  151.206135 -33.729484    1614.3           0 2020-04-20 23:02:24.100
1  23:2:24.2   10.9  32.0  180.0  16.7 -29.5  -0.0  151.206135 -33.729484    1614.3           0 2020-04-20 23:02:24.200
2  23:2:24.3   10.9  32.0  180.0  16.7 -29.5  -0.0  151.206135 -33.729484    1614.3           0 2020-04-20 23:02:24.300

编辑:添加时区转换

由于听起来你的日期和时间不同步,你必须将时间调整到你的日期应该在的时区,然后输入日期

  1. 使用pytz库:
from datetime import datetime
import pytz

def parse_dt(input_ts, target_tz, year, month, day):
    ts = datetime.strptime(f'{input_ts}', '%H:%M:%S.%f') # defaults to UTC
    ts_adj = ts.astimezone(pytz.timezone(target_tz)) # convert to time zone
    out_dt = ts_adj.replace(year=year, month=month, day=day)
    return(out_dt)

这将产生:

In [100]: parse_dt('23:2:24.1', 'EST', 2020, 4, 20)
Out[100]: datetime.datetime(2020, 4, 20, 2, 2, 24, 100000, tzinfo=<StaticTzInfo 'EST'>)
  1. 使用来自datetimetimedelta
from datetime import datetime, timedelta

def parse_dt(input_ts, ts_offset, year, month, day):
    ts = datetime.strptime(f'{input_ts}', '%H:%M:%S.%f') # defaults to UTC
    ts_adj = ts - timedelta(hours=ts_offset) # subtract x hours
    out_dt = ts_adj.replace(year=year, month=month, day=day)
    return(out_dt)

产生

In [108]: parse_dt('23:2:24.1', 6, 2020, 4, 20)
Out[108]: datetime.datetime(2020, 4, 20, 17, 2, 24, 100000)

相关问题 更多 >