[英]Pandas: Convert Timestamp to datetime.date
I have a pandas column of Timestamp data我有一个 pandas 时间戳数据列
In [27]: train["Original_Quote_Date"][6]
Out[27]: Timestamp('2013-12-25 00:00:00')
How can check equivalence of these objects to datetime.date
objects of the type如何检查这些对象与
datetime.date
类型对象的等价性
datetime.date(2013, 12, 25)
I have a pandas column of Timestamp data我有一个时间戳数据的熊猫列
In [27]: train["Original_Quote_Date"][6]
Out[27]: Timestamp('2013-12-25 00:00:00')
How can check equivalence of these objects to datetime.date
objects of the type如何检查这些对象与该类型的
datetime.date
对象的等效性
datetime.date(2013, 12, 25)
I have a pandas column of Timestamp data我有一个时间戳数据的熊猫列
In [27]: train["Original_Quote_Date"][6]
Out[27]: Timestamp('2013-12-25 00:00:00')
How can check equivalence of these objects to datetime.date
objects of the type如何检查这些对象与该类型的
datetime.date
对象的等效性
datetime.date(2013, 12, 25)
I have a pandas column of Timestamp data我有一个时间戳数据的熊猫列
In [27]: train["Original_Quote_Date"][6]
Out[27]: Timestamp('2013-12-25 00:00:00')
How can check equivalence of these objects to datetime.date
objects of the type如何检查这些对象与该类型的
datetime.date
对象的等效性
datetime.date(2013, 12, 25)
I have a pandas column of Timestamp data我有一个时间戳数据的熊猫列
In [27]: train["Original_Quote_Date"][6]
Out[27]: Timestamp('2013-12-25 00:00:00')
How can check equivalence of these objects to datetime.date
objects of the type如何检查这些对象与该类型的
datetime.date
对象的等效性
datetime.date(2013, 12, 25)
So, got this from an IBM coursera tutorial.所以,从 IBM coursera 教程中得到了这个。
data['date'] = data['TimeStamp'].apply(lambda d: datetime.date.fromtimestamp(d\/1000.0)) data['date'] = data['TimeStamp'].apply(lambda d: datetime.date.fromtimestamp(d\/1000.0))
I've used the way recommended by Filomeno Gonzalez, albeit with a slight twist:我使用了 Filomeno Gonzalez 推荐的方式,尽管略有不同:
data['date'] = data['date'].apply(lambda x: x.date())
If I have a pandas DataFrame with timestamp column ( 1546300800000, 1546301100000, 1546301400000, 1546301700000, 1546302000000
) and I want to convert this into date time format如果我有一个带有时间戳列的
1546300800000, 1546301100000, 1546301400000, 1546301700000, 1546302000000
),我想将其转换为日期时间格式
import datetime
df['date'] = df['date'].apply(lambda x: datetime.datetime.fromtimestamp(x/1000.0))
This will return a column with the format 2019-01-01 00:00:00, 2019-01-01 00:05:00, 2019-01-01 00:10:00, 2019-01-01 00:15:00, 2019-01-01 00:20:00
...etc这将返回格式
2019-01-01 00:00:00, 2019-01-01 00:05:00, 2019-01-01 00:10:00, 2019-01-01 00:15:00, 2019-01-01 00:20:00
...等
Dividing by 1000 to convert from ms to s as explained here如此处所述,除以 1000 以从 ms 转换为 s
In my case, I had the epochs as a normal "column" (ie it wasn't a TimeSeries
).在我的例子中,我将时代作为一个普通的“列”(即它不是
TimeSeries
)。
I converted it as follows:我将其转换如下:
import pandas as pd
df['Timestamp'] = pd.to_datetime(df['Timestamp'] / 1000, unit='s')
Time was in milliseconds, so I needed to divide by a thousand.时间以毫秒为单位,所以我需要除以一千。 I set the
unit='s'
to mention that the Timestamp
is in seconds (you can, however, use unit='ms'
if you don't want to divide by 1000, or depending on your original Timestamp
unit).我设置
unit='s'
以提及Timestamp
以秒为单位(但是,如果您不想除以 1000,或者取决于您的原始Timestamp
单位,您可以使用unit='ms'
)。
1673885284000 becomes 2023-01-16 4:08:04
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.