pandas Python:日期时间到季节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44124436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: Datetime to season
提问by Jespar
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.
我想将日期时间序列转换为季节,例如对于 3、4、5 个月,我想用 2(春季)替换它们;对于第 6、7、8 个月,我想用 3(夏季)等替换它们。
So, I have this series
所以,我有这个系列
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
and
和
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: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
ValueError:包含多个元素的数组的真值不明确。使用 a.any() 或 a.all()
While in the second is a large list with errors. Any ideas please? Thanks
而在第二个是一个包含错误的大列表。请问有什么想法吗?谢谢
回答by AChampion
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]
So for your use-case using vector operations (credit @DSM):
因此,对于您使用向量操作的用例(信用@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
回答by Mohamed Ali JAMAOUI
It's, also, possible to use dictionary mapping.
也可以使用字典映射。
Create a dictionary that maps a month to a season:
In [27]: seasons = [1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 1] In [28]: month_to_season = dict(zip(range(1,13), seasons)) In [29]: month_to_season Out[29]: {1: 1, 2: 1, 3: 2, 4: 2, 5: 2, 6: 3, 7: 3, 8: 3, 9: 4, 10: 4, 11: 4, 12: 1}
Use it to convert the months to seasons
In [30]: df.id.dt.month.map(month_to_season) Out[30]: 1 3 2 3 3 3 4 4 5 4 6 4 7 4 8 4 Name: id, dtype: int64
创建一个将一个月映射到一个季节的字典:
In [27]: seasons = [1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 1] In [28]: month_to_season = dict(zip(range(1,13), seasons)) In [29]: month_to_season Out[29]: {1: 1, 2: 1, 3: 2, 4: 2, 5: 2, 6: 3, 7: 3, 8: 3, 9: 4, 10: 4, 11: 4, 12: 1}
用它来将月份转换为季节
In [30]: df.id.dt.month.map(month_to_season) Out[30]: 1 3 2 3 3 3 4 4 5 4 6 4 7 4 8 4 Name: id, dtype: int64
Performance: This is fairly fast
性能:这相当快
In [35]: %timeit df.id.dt.month.map(month_to_season)
1000 loops, best of 3: 422 μs per loop
回答by John Hao
I think this would work.
我认为这会奏效。
while True:
date=int(input("Date?"))
season=""
if date<4:
season=1
elif date<7:
season=2
elif date<10:
season=3
elif date<13:
season=4
else:
print("This would not work.")
print(season)