Python:日期到季节 [英] Python: Datetime to season
问题描述
我想将日期时间序列转换为季节,例如对于第3、4、5个月,我想将其替换为2(春季);对于6、7、8个月,我想用3(夏季)等替换它们.
I want to convert a date time series to season, for example for months 3, 4, 5 I want to replace them with 2 (spring); for months 6, 7, 8 I want to replace them with 3 (summer) etc.
所以,我有这个系列
id
1 2011-08-20
2 2011-08-23
3 2011-08-27
4 2011-09-01
5 2011-09-05
6 2011-09-06
7 2011-09-08
8 2011-09-09
Name: timestamp, dtype: datetime64[ns]
这是我一直在尝试使用的代码,但无济于事.
and this is the code I have been trying to use, but to no avail.
# Get seasons
spring = range(3, 5)
summer = range(6, 8)
fall = range(9, 11)
# winter = everything else
month = temp2.dt.month
season=[]
for _ in range(len(month)):
if any(x == spring for x in month):
season.append(2) # spring
elif any(x == summer for x in month):
season.append(3) # summer
elif any(x == fall for x in month):
season.append(4) # fall
else:
season.append(1) # winter
和
for _ in range(len(month)):
if month[_] == 3 or month[_] == 4 or month[_] == 5:
season.append(2) # spring
elif month[_] == 6 or month[_] == 7 or month[_] == 8:
season.append(3) # summer
elif month[_] == 9 or month[_] == 10 or month[_] == 11:
season.append(4) # fall
else:
season.append(1) # winter
这两种解决方案都不起作用,特别是在第一个实现中,我收到一个错误:
Neither solution works, specifically in the first implementation I receive an error:
ValueError:具有多个元素的数组的真值是 模糊的.使用a.any()或a.all()
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
第二个是有错误的大列表.有什么想法吗?谢谢
While in the second is a large list with errors. Any ideas please? Thanks
推荐答案
您可以使用简单的数学公式将一个月压缩为一个季节,例如:
You can use a simple mathematical formula to compress a month to a season, e.g.:
>>> [(month%12 + 3)//3 for month in range(1, 13)]
[1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 1]
因此对于使用矢量运算的用例(@DSM信用):
So for your use-case using vector operations (credit @DSM):
>>> (temp2.dt.month%12 + 3)//3
1 3
2 3
3 3
4 4
5 4
6 4
7 4
8 4
Name: id, dtype: int64
这篇关于Python:日期到季节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!