【问题标题】:Convert date to timestamp using lambda function while reading the file在读取文件时使用 lambda 函数将日期转换为时间戳
【发布时间】:2016-09-12 05:33:41
【问题描述】:

我正在读取包含这种格式日期的 csv 文件:

date
01/05/2014
01/05/2014
01/05/2014
01/05/2014
01/05/2014
01/05/2014
01/05/2014
01/05/2014
01/05/2014

我不能使用这样的字符串格式的日期,我需要将其转换为数字时间戳。

所以我写了这段代码:

Train = pd.read_csv("train.tsv", sep='\t') 
Train['timestamp'] = pd.to_datetime(Train['date']).apply(lambda a: a.timestamp())

这个给我:

Train['timestamp'] = pd.to_datetime(Train['date']).apply(lambda a: a.timestamp())
AttributeError: 'Timestamp' 对象没有属性 'timestamp'

能否请您纠正我在 lambda 中输入时间戳?

编辑代码:

Train = pd.read_csv("data_scientist_assignment.tsv", sep='\t', parse_dates=['date'])
#print df.head()
# Train['timestamp'] = pd.to_datetime(Train['date']).apply(lambda a: a.timestamp())
Train['timestamp'] = Train.date.values.astype(np.int64)
x1=["timestamp", "hr_of_day"]
test=pd.read_csv("test.csv")
print(Train.columns)
print(test.columns)
model = LogisticRegression()
model.fit(Train[x1], Train["vals"])
print(model)
print model.score(Train[x1], Train["vals"])

【问题讨论】:

    标签: python csv pandas lambda timestamp


    【解决方案1】:

    您需要将参数parse_dates 添加到read_csv,并将列名转换为datetime

    import pandas as pd
    import io
    
    temp=u"""date
    01/05/2014
    01/05/2014
    01/05/2014
    01/05/2014
    01/05/2014
    01/05/2014
    01/05/2014
    01/05/2014
    01/05/2014"""
    #after testing replace io.StringIO(temp) to filename
    df = pd.read_csv(io.StringIO(temp), sep='\t', parse_dates=['date'])
    
    print (df)
            date
    0 2014-01-05
    1 2014-01-05
    2 2014-01-05
    3 2014-01-05
    4 2014-01-05
    5 2014-01-05
    6 2014-01-05
    7 2014-01-05
    8 2014-01-05
    
    print (df.dtypes)
    date    datetime64[ns]
    dtype: object
    

    另一种解决方案是为列date 的顺序添加数字 - 在示例中它是第一列,因此添加0(python 从0 计数):

    df = pd.read_csv(io.StringIO(temp), sep='\t', parse_dates=[0])
    
    print (df)
            date
    0 2014-01-05
    1 2014-01-05
    2 2014-01-05
    3 2014-01-05
    4 2014-01-05
    5 2014-01-05
    6 2014-01-05
    7 2014-01-05
    8 2014-01-05
    
    print (df.dtypes)
    date    datetime64[ns]
    dtype: object
    

    然后需要通过values将列转换为numpy array并转换为int

    #unix time in ns
    df.date = df.date.values.astype(np.int64)
    print (df)
                      date
    0  1388880000000000000
    1  1388880000000000000
    2  1388880000000000000
    3  1388880000000000000
    4  1388880000000000000
    5  1388880000000000000
    6  1388880000000000000
    7  1388880000000000000
    8  1388880000000000000
    
    #unix time in us
    df.date = df.date.values.astype(np.int64) // 1000
    print (df)
                   date
    0  1388880000000000
    1  1388880000000000
    2  1388880000000000
    3  1388880000000000
    4  1388880000000000
    5  1388880000000000
    6  1388880000000000
    7  1388880000000000
    8  1388880000000000
    
    #unix time in ms
    df.date = df.date.values.astype(np.int64) // 1000000
    #df.date = pd.to_datetime(df.date, unit='ms')
    print (df)
                date
    0  1388880000000
    1  1388880000000
    2  1388880000000
    3  1388880000000
    4  1388880000000
    5  1388880000000
    6  1388880000000
    7  1388880000000
    8  1388880000000
    
    #unix time in s
    df.date = df.date.values.astype(np.int64) // 1000000000
    print (df)
             date
    0  1388880000
    1  1388880000
    2  1388880000
    3  1388880000
    4  1388880000
    5  1388880000
    6  1388880000
    7  1388880000
    8  1388880000
    

    【讨论】:

    • 谢谢,但我的下一个操作仍然失败说'Timestamp' object has no attribute 'timestamp'
    • 是的,我添加了解决方案,请检查一下。
    【解决方案2】:

    另一种解决方法是使用to_datetime()

    In [209]: df['date']
    Out[209]: 
    0    01/05/2014
    1    01/05/2014
    2    01/05/2014
    3    01/05/2014
    4    01/05/2014
    5    01/05/2014
    6    01/05/2014
    7    01/05/2014
    8    01/05/2014
    Name: date, dtype: object
    
    In [210]: df['date'] = pd.to_datetime(df['date'])
    
    In [211]: df['date']
    Out[211]: 
    0   2014-01-05
    1   2014-01-05
    2   2014-01-05
    3   2014-01-05
    4   2014-01-05
    5   2014-01-05
    6   2014-01-05
    7   2014-01-05
    8   2014-01-05
    Name: date, dtype: datetime64[ns]
    

    此外,您可以像这样获得秒数:

    In [232]: df['date'].astype(pd.np.int64) // 10**9
    Out[232]: 
    0    1388880000
    1    1388880000
    2    1388880000
    3    1388880000
    4    1388880000
    5    1388880000
    6    1388880000
    7    1388880000
    8    1388880000
    Name: date, dtype: int64
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 2020-10-19
      • 2011-07-09
      • 2017-04-25
      • 2022-01-08
      相关资源
      最近更新 更多