如何从Python datetime对象中获取带有时区信息的年份、月份、日期、小时等信息?

4

我一直在使用Python中的datetime库中的datetimes,并使用pytz将它们变成时区感知的。然后,我将它们用作Pandas DataFrames中的日期,并尝试使用Pandas的apply函数和datetimes的".day"、".hour"、".minute"等方法来创建只有日、小时或分钟的列。令人惊讶的是,它返回了UTC值。有没有办法返回本地的日、小时或分钟?简单地添加偏移量是不够的,因为UTC到夏令时的偏移量会发生变化。

非常感谢!

这是一个关于我的问题的例子:

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

注意,start + timedelta() 可能会跨越夏令时边界,即你可能需要使用 tz.normalize() 调用来获取正确的本地小时。@JulesMazur:另一个问题不涉及 pandas - jfs
1个回答

2
使用列表推导式似乎可以解决问题:
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

2
惯用的方法是使用.dt访问器,例如shapes['actuals dates'].dt.hour。 - Jeff

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接