如何从Python日期时间获得时区感知的年,月,日,小时等?

时间:2016-04-18 23:09:36

标签: python datetime pandas dataframe

我一直在Python中使用日期时间库中的日期时间,并使用pytz使它们能够识别时区。我曾经在Pandas DataFrames中使用它们作为日期,并试图使用Pandas的应用功能和" .day"," .hour"," .minute& #34;等日期时间的方法,用于创建仅包含日,小时或分钟的列。令人惊讶的是,它给出了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

1 个答案:

答案 0 :(得分:1)

使用列表理解似乎可以解决问题:

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