如何将pandas时间戳更改为python datetime对象?

问题描述 投票:0回答:1

我正在寻找一种方法将pandas时间戳更改为python datetime对象,但我失败了。我用过to_pydatetime()。

以下是我的评论代码:

import pandas
import datetime
import pytz

forced_UTC = pytz.timezone("Europe/London").localize(datetime.datetime(2019, 1, 30, 9, 5)).tzinfo
forced_BST = pytz.timezone("Europe/London").localize(datetime.datetime(2019, 4, 27, 9, 5)).tzinfo
#print (forced_UTC, forced_BST)

# Create dataframe and check the value of the first element in the column.
my_df = pandas.DataFrame({"my_column": ["2019-04-01 00:15:00", "2019-02-23 13:00:00", "2019-02-23 14:00:00"]})
first_date = my_df["my_column"].iloc[0]
print (first_date, type(first_date)) # 2019-04-01 00:15:00 <class 'str'>

# Change every element in the column from string to datetime object.
my_df["my_column"] = [datetime.datetime.strptime(element, "%Y-%m-%d %H:%M:%S").replace(tzinfo=forced_UTC) for element in my_df["my_column"]]
first_date = my_df["my_column"].iloc[0]
print (first_date, type(first_date)) # <class 'pandas._libs.tslibs.timestamps.Timestamp'>
# Didn't work, thought it would make them python datetime objects.

# Try creating a new list and replacing the column.
list_to_replace = [element.to_pydatetime() for element in my_df["my_column"]] # make all timestamps datetimes
print (list_to_replace[0],type(list_to_replace[0])) # 2019-04-01 01:15:00+01:00 <class 'datetime.datetime'>
my_df["my_column"] = [element for element in list_to_replace]
first_date = my_df["my_column"].iloc[0] 
print (first_date, type(first_date))# 2019-04-01 01:15:00+01:00 <class pandas._libs.tslibs.timestamps.Timestamp'>
# Didn't work.

# Use to_pydatetime() doesn't work either
print (first_date.to_pydatetime(), type(first_date)) # 2019-04-01 01:15:00+01:00 <class 'pandas._libs.tslibs.timestamps.Timestamp'>

# Using to_pydatetime() like this works!?!?!?!
first_date = my_df["my_column"].iloc[0].to_pydatetime()
print (first_date, type(first_date)) # 2019-04-01 01:15:00+01:00 <class 'datetime.datetime'>

# print (datetime.datetime.strftime(first_date, "%H"))
# print (datetime.datetime(2019, 4, 1, 0, 15, tzinfo=forced_BST) > first_date)

任何人都可以看到我做错了什么?

python pandas datetime timestamp pytz
1个回答
0
投票

它工作,但你打印type而不修改类型。 在这里,你打印的确是熊猫日期(first_date)和熊猫类型(type(first_date))。

first_date = my_df["my_column"].iloc[0] 
print (first_date, type(first_date)) # 2019-04-01 01:15:00+01:00 <class pandas._libs.tslibs.timestamps.Timestamp'>
# Didn't work.

在这种情况下,你打印一个python日期(first_date.to_pydatetime()),因为你确实使用to_pydatetime,但first_date变量从未被修改,所以它的type仍然是一个熊猫日期。

print (first_date.to_pydatetime(), type(first_date)) # 2019-04-01 01:15:00+01:00 <class 'pandas._libs.tslibs.timestamps.Timestamp'>

在这里你也用python日期覆盖first_date变量,(first_date = first_date.to_pydatetime())这就是为什么你认为它只在那时起作用的原因。所以现在first_date将是一个python日期,因此type(first_date)将返回你的预期

# Using to_pydatetime() like this works!?!?!?!
first_date = my_df["my_column"].iloc[0].to_pydatetime()
print (first_date, type(first_date)) # 2019-04-01 01:15:00+01:00 <class 'datetime.datetime'>
© www.soinside.com 2019 - 2024. All rights reserved.