如何从Python datetime获取时区感知的年、月、日、小时等?
我一直在使用Python中datetime库中的datetimes,并使用pytz使它们具有时区意识。然后,我在Pandas数据框中使用它们作为日期,并尝试使用Pandas的apply函数和datetimes的“.day”、“.hour”、“.minute”等方法创建只包含日期、小时或分钟的列。令人惊讶的是,它给出了UTC值。有没有办法返回当地的日期、小时或分钟?仅仅添加偏移量是不够的,因为UTC的偏移量随着夏令时的变化而变化 非常感谢 下面是我所说的一个例子:如何从Python datetime获取时区感知的年、月、日、小时等?,python,datetime,pandas,dataframe,Python,Datetime,Pandas,Dataframe,我一直在使用Python中datetime库中的datetimes,并使用pytz使它们具有时区意识。然后,我在Pandas数据框中使用它们作为日期,并尝试使用Pandas的apply函数和datetimes的“.day”、“.hour”、“.minute”等方法创建只包含日期、小时或分钟的列。令人惊讶的是,它给出了UTC值。有没有办法返回当地的日期、小时或分钟?仅仅添加偏移量是不够的,因为UTC的偏移量随着夏令时的变化而变化 非常感谢 下面是我所说的一个例子: import pandas as
import pandas as pd
import datetime as dt
import pytz
# Simply return the hour of a date
def get_hour(dt1):
return dt1.hour
# Create a date column to segment by month
# Create the date list
PST = pytz.timezone('US/Pacific')
start = PST.localize(dt.datetime(2016, 1, 1))
actuals_dates = [start + dt.timedelta(hours=x) for x in range(8760)]
# Outside of this context, you can get the hour
print ''
print 'Hour at the start date:'
print get_hour(start)
print ''
#add it to a pandas DataFrame as a column
shapes = pd.DataFrame()
shapes['actuals dates'] = actuals_dates
# create a column for the hour
shapes['actuals hour'] = shapes['actuals dates'].apply(get_hour)
# Print the first 24 hours
print shapes.head(24)
将返回:
Hour at the start date:
0
actuals dates actuals hour
0 2016-01-01 00:00:00-08:00 8
1 2016-01-01 01:00:00-08:00 9
2 2016-01-01 02:00:00-08:00 10
3 2016-01-01 03:00:00-08:00 11
4 2016-01-01 04:00:00-08:00 12
5 2016-01-01 05:00:00-08:00 13
6 2016-01-01 06:00:00-08:00 14
7 2016-01-01 07:00:00-08:00 15
8 2016-01-01 08:00:00-08:00 16
9 2016-01-01 09:00:00-08:00 17
10 2016-01-01 10:00:00-08:00 18
11 2016-01-01 11:00:00-08:00 19
12 2016-01-01 12:00:00-08:00 20
13 2016-01-01 13:00:00-08:00 21
14 2016-01-01 14:00:00-08:00 22
15 2016-01-01 15:00:00-08:00 23
16 2016-01-01 16:00:00-08:00 0
17 2016-01-01 17:00:00-08:00 1
18 2016-01-01 18:00:00-08:00 2
19 2016-01-01 19:00:00-08:00 3
20 2016-01-01 20:00:00-08:00 4
21 2016-01-01 21:00:00-08:00 5
22 2016-01-01 22:00:00-08:00 6
23 2016-01-01 23:00:00-08:00 7
使用列表理解似乎可以达到以下目的:
shapes['hour'] = [ts.hour for ts in shapes['actuals dates']]
shapes.head()
actuals dates actuals hour hour
0 2016-01-01 00:00:00-08:00 8 0
1 2016-01-01 01:00:00-08:00 9 1
2 2016-01-01 02:00:00-08:00 10 2
3 2016-01-01 03:00:00-08:00 11 3
4 2016-01-01 04:00:00-08:00 12 4
根据@Jeff的提醒,您还可以使用dt
访问器功能,例如:
>>> shapes['actuals dates'].dt.hour.head()
0 0
1 1
2 2
3 3
4 4
Name: actuals dates, dtype: int64
可能重复的,
start+timedelta()
可能会跨越DST边界,即您可能需要调用tz.normalize()
来获取正确的本地小时数@JulesMazur:另一个问题不涉及熊猫。惯用用法是使用.dt访问器,例如shapes['actuals dates'].dt.hour