pandas - 将包含字符串和整数的列解析为日期时间

1 投票
1 回答
758 浏览
提问于 2025-04-18 14:02

我有一些数据,其中一列,比如说第0列,包含像'%Y-%m-%d %H:%M:%S'这样的字符串,另一列,比如说第1列,包含以百秒为单位的整数。我想把这两列一起读入数据框的索引中。

parse = lambda d: dt.datetime.strptime(d,'%Y-%m-%d %H:%M:%S %f')
df = pd.read_csv(myFile, sep=';', index_col=0, parse_dates=[[0,1]], \
                 keep_date_col=True, date_parser=parse)

但是,使用这种方法会把所有的整数1, 2,...9都当成是10, 20,...90百秒。例如,第0列的'2013-3-27 09:00:01'和第1列的9会被转换成Timestamp('2013-03-27 09:00:01.900000', tz=None),而不是Timestamp('2013-03-27 09:00:01.090000', tz=None)。

我猜测date_parser函数把9当成了'9',但我需要把它理解为'09'。我该怎么解决这个问题呢?

编辑:

df = pd.read_csv(myFile, sep=';') 
# with column 'TIMESTAMP' containing the strings and column 'HSEC' containing \
# the ints with the hundreds of seconds

df['newTimestamp'] = pd.to_datetime(df['TIMESTAMP'],format='%Y-%m-%d %H:%M:%S').add(pd.to_timedelta(dataOB['HSEC']*10000000) 
dataOB.set_index('new',inplace=True)
dataOB.sort_index(inplace=True)

(奇怪的是,解决方案往往是在我在这里发问之后才会浮现出来,尽管在发问之前我已经找了几个小时。希望这对其他人也有用。)

1 个回答

1

一些示例数据

df = pd.read_csv(StringIO("""col1;col2;col3
    2014-07-16 14:23:46;1;12
    2014-07-16 14:23:53;5;12
    2014-07-16 14:23:55;10;12
    2014-07-16 14:23:59;15;12
    2014-07-16 14:23:59;20;12
    2014-07-16 14:24:00;25;12"""), sep=';')

与其在read_csv这一步就处理所有内容,不如先读取数据,然后再把列合并,像这样?

df['date'] = df['col1'] + '.' +  df['col2'].apply(lambda x: str(x).zfill(2))

然后你可以把合并后的列传给pd.to_datetime,并设置你的索引。

df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')

撰写回答