【问题标题】:Python: from timestamp to datetime [duplicate]Python:从时间戳到日期时间
【发布时间】:2017-05-31 10:53:32
【问题描述】:

我有以下数据框:

df = pd.DataFrame({'user': ['Andrea', 'Gioele'],
                    'year': [1983, 2014],
                    'month': [11, 1],
                    'day': [8, 11]} )

然后我以两种方式为每一行创建日期。 第一:

df['dateA'] = df.apply(lambda x: datetime.date(x['year'],x['month'],x['day']), axis=1)

第二:

df['dateB'] = pd.to_datetime(df[['year','month','day']])

我有以下数据框:

>>> df
10:    day  month  user   year      dateA      dateB
0       8     11  Andrea  1983  1983-11-08  1983-11-08
1      11      1  Gioele  2014  2014-01-11  2014-01-11

我有两种不同的格式:

>>> df['dateA']
1983-11-08
2014-01-11
Name: dateA, dtype: object
>>> df['dateB']
1983-11-08
2014-01-11
Name: dateB, dtype: datetime64[ns]

此外:

>>> df['dateA'].iloc[0]
datetime.date(1983, 11, 8)
>>> df['dateB'].iloc[0]
Timestamp('1983-11-08 00:00:00')

问题是使用第一种方法计算日期非常昂贵,所以我想转换 df['dateB'] 使其具有“对象”格式。有什么办法吗?

注意:我已经尝试过可能的“重复问题”建议(它们总是有字符串,而不是时间戳),但我得到了以下内容

>>> datetime.datetime.fromtimestamp(df['dateB'].iloc[0])
Traceback (most recent call last):
  File "<pyshell#68>", line 1, in <module>
    datetime.datetime.fromtimestamp(df['dateB'].iloc[0])
TypeError: a float is required

【问题讨论】:

标签: python date datetime pandas python-object


【解决方案1】:

我觉得你可以用dt.date:

df['dateB'] = pd.to_datetime(df[['year','month','day']]).dt.date

print (df['dateB'].dtype)
object

print (type(df['dateB'].iloc[0]))
<class 'datetime.date'>

【讨论】:

    猜你喜欢
    • 2015-04-25
    • 2021-01-28
    • 1970-01-01
    • 2013-06-03
    • 2022-01-03
    • 2017-04-29
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多