Python 在多行中访问datetime时间戳的值-语法?

Python 在多行中访问datetime时间戳的值-语法?,python,pandas,Python,Pandas,我有一列日期,我需要能够访问逻辑操作中的值。对于一行,我可以很容易地做到这一点,但我不知道让我查看所有行的语法 我试图找到某个时间范围内和某个位置的所有值 Y=x.loc[xx.timestamp[:].month<7 and xx.timestamp[:].month>2 and xx.Area == 'GBK'] 访问此行的时间戳月份值 xx.timestamp[10].month Out[292]: 2 访问所有行的时间戳月份值 xx.timestamp[:].month

我有一列日期,我需要能够访问逻辑操作中的值。对于一行,我可以很容易地做到这一点,但我不知道让我查看所有行的语法

我试图找到某个时间范围内和某个位置的所有值

Y=x.loc[xx.timestamp[:].month<7 and xx.timestamp[:].month>2 and xx.Area == 'GBK']
访问此行的时间戳月份值

xx.timestamp[10].month
Out[292]: 2
访问所有行的时间戳月份值

xx.timestamp[:].month
Traceback (most recent call last):

AttributeError: 'Series' object has no attribute 'month'

我可以访问一行,但不能访问更多。我错过了什么

序列没有属性“月”(即使是日期时间序列):

您可以将该系列包装为DatetimeIndex,它不具有月份属性:

In [13]: pd.DatetimeIndex(s).month
Out[13]: array([1])
在0.15.0中,将有一个dt访问器:

In [14]: s.dt.month  # 0.15.0+ only
Out[14]:
0    1
dtype: int64

注意:您不能将
与系列或numpy数组一起使用,您需要使用
&

x.loc[(DatetimeIndex(xx.timestamp).month < 7) & (DatetimeIndex(xx.timestamp.month) > 2) & (xx.Area == 'GBK')]
x.loc[(DatetimeIndex(xx.timestamp.month)<7)和(DatetimeIndex(xx.timestamp.month)>2)和(xx.Area='GBK')]

非常感谢您的回答。在您的示例中,为什么s.month或s[:].month不起作用时s[0].month起作用(Out[297]:1)?如果一个系列中只有一个元素被调用,那么它不是一个系列吗?即使这个系列只有一个元素长?@Ryan系列中的一个元素是一个时间戳,它有一个月属性。
In [14]: s.dt.month  # 0.15.0+ only
Out[14]:
0    1
dtype: int64
x.loc[(DatetimeIndex(xx.timestamp).month < 7) & (DatetimeIndex(xx.timestamp.month) > 2) & (xx.Area == 'GBK')]