Python在read_csv中组合时间戳列和fillna

Python在read_csv中组合时间戳列和fillna,python,csv,numpy,pandas,timestamp,Python,Csv,Numpy,Pandas,Timestamp,我正在读一个有熊猫的csv文件。格式为: Date Time x1 x2 x3 x4 x5 3/7/2012 11:09:22 13.5 2.3 0.4 7.3 6.4 12.6 3.4 9.0 3.0 7.0 3.6 4.4 8.0 6.0 5.0

我正在读一个有熊猫的csv文件。格式为:

Date        Time        x1      x2     x3     x4    x5
3/7/2012    11:09:22    13.5    2.3    0.4    7.3   6.4
                        12.6    3.4    9.0    3.0   7.0
                        3.6     4.4    8.0    6.0   5.0
                        10.6    3.5    1.0    3.0   8.0
...
3/7/2012    11:09:23    10.5    23.2   0.3    7.8   4.4
                        11.6    13.4   19.0   13.0  17.0
...
如您所见,并非每一行都有时间戳。没有时间戳的每一行都与上面有时间戳的最近行具有相同的1秒间隔

我试着做3件事: 1.合并日期和时间列以获得单个时间戳列。 2.将该列转换为秒单位。 3.填充空单元格以具有适当的时间戳。 所需的最终结果是一个数组,每行都有时间戳(以秒为单位)

我不知道如何快速地将时间戳转换为秒的单位,然后执行慢速for循环并使用Python内置的time.mktime方法

然后,当我填写缺少的时间戳值时,问题是日期和时间列中没有时间戳的单元格都会得到一个“nan”值,合并后会得到一个值为“nan-nan”的单元格。然后,当我使用fillna()方法时,它不会将“nan-nan”解释为nan

我正在使用以下代码获得问题结果(不包括尝试转换为秒的部分):


谢谢你的帮助。

假设你从1900年1月1日起需要几秒钟

import pandas
from io import StringIO
import datetime
data = StringIO("""\
Date,Time,x1,x2,x3,x4,x5
3/7/2012,11:09:22,13.5,2.3,0.4,7.3,6.4
,,12.6,3.4,9.0,3.0,7.0
,,3.6,4.4,8.0,6.0,5.0
,,10.6,3.5,1.0,3.0,8.0
3/7/2012,11:09:23,10.5,23.2,0.3,7.8,4.4
,,11.6,13.4,19.0,13.0,17.0
""")

df = pandas.read_csv(data, parse_dates=['Date']).fillna(method='ffill')

def dealwithdates(row):
    datestring = row['Date'].strftime('%Y-%m-%d')
    dtstring = '{} {}'.format(datestring, row['Time'])
    date = datetime.datetime.strptime(dtstring, '%Y-%m-%d %H:%M:%S')

    refdate = datetime.datetime(1900, 1, 1)
    return (date - refdate).total_seconds()

df['ordinal'] = df.apply(dealwithdates, axis=1)
print(df)

        Date      Time    x1    x2    x3    x4    x5     ordinal
0 2012-03-07  11:09:22  13.5   2.3   0.4   7.3   6.4  3540107362
1 2012-03-07  11:09:22  12.6   3.4   9.0   3.0   7.0  3540107362
2 2012-03-07  11:09:22   3.6   4.4   8.0   6.0   5.0  3540107362
3 2012-03-07  11:09:22  10.6   3.5   1.0   3.0   8.0  3540107362
4 2012-03-07  11:09:23  10.5  23.2   0.3   7.8   4.4  3540107363
5 2012-03-07  11:09:23  11.6  13.4  19.0  13.0  17.0  3540107363
import pandas
from io import StringIO
import datetime
data = StringIO("""\
Date,Time,x1,x2,x3,x4,x5
3/7/2012,11:09:22,13.5,2.3,0.4,7.3,6.4
,,12.6,3.4,9.0,3.0,7.0
,,3.6,4.4,8.0,6.0,5.0
,,10.6,3.5,1.0,3.0,8.0
3/7/2012,11:09:23,10.5,23.2,0.3,7.8,4.4
,,11.6,13.4,19.0,13.0,17.0
""")

df = pandas.read_csv(data, parse_dates=['Date']).fillna(method='ffill')

def dealwithdates(row):
    datestring = row['Date'].strftime('%Y-%m-%d')
    dtstring = '{} {}'.format(datestring, row['Time'])
    date = datetime.datetime.strptime(dtstring, '%Y-%m-%d %H:%M:%S')

    refdate = datetime.datetime(1900, 1, 1)
    return (date - refdate).total_seconds()

df['ordinal'] = df.apply(dealwithdates, axis=1)
print(df)

        Date      Time    x1    x2    x3    x4    x5     ordinal
0 2012-03-07  11:09:22  13.5   2.3   0.4   7.3   6.4  3540107362
1 2012-03-07  11:09:22  12.6   3.4   9.0   3.0   7.0  3540107362
2 2012-03-07  11:09:22   3.6   4.4   8.0   6.0   5.0  3540107362
3 2012-03-07  11:09:22  10.6   3.5   1.0   3.0   8.0  3540107362
4 2012-03-07  11:09:23  10.5  23.2   0.3   7.8   4.4  3540107363
5 2012-03-07  11:09:23  11.6  13.4  19.0  13.0  17.0  3540107363