英文:
How to assign a value by month for timeseries data?
问题
我认为我应该使用groupby(),但我不确定如何在时间序列上使用它。有人知道如何在这里使用"timestamp"特征来推导"value"列吗?
英文:
I have a time series data to which I am trying to assign a value based on which month the sample falls in. Here is an example of what I am looking for:
Timestamp | Value |
---|---|
29-12-2018 | 1 |
31-12-2018 | 1 |
01-01-2019 | 2 |
05-01-2019 | 2 |
02-02-2018 | 3 |
I think I should be using groupby(), but I was not sure how to do that for a timeseries. Anyone know how use timestamp
feature here to derive the value
column?
答案1
得分: 1
将Timestamp
从日期时间转换为周期,然后进行分组,帮助我分配数值。下面是上述数据框所需的代码:
df["Period"] = df['Timestamp'].dt.to_period('M')
df["Value"] = df.groupby(["Period"]).ngroup() + 1
所以,我创建了一个名为Period
的新列,然后在其上使用了groupby()和ngroup()方法来对其进行分组并分配一个唯一值给每个周期,并存储在Value
列中。
Timestamp | Period | Value |
---|---|---|
29-12-2018 | 12-2018 | 1 |
31-12-2018 | 12-2018 | 1 |
01-01-2019 | 01-2019 | 2 |
05-01-2019 | 01-2019 | 2 |
02-02-2018 | 02-2019 | 3 |
附:在创建Value
时我加了1,因为分组是从0开始分配的,但我希望分组从1开始。
英文:
Converting the Timestamp
from datetime to period and then grouping them helped me assign the values. Below is the required code for above dataframe:
df["Period"] = df['Timestamp'].dt.to_period('M')
df["Value"] = df.groupby(["Period"]).ngroup() + 1
So, I created a new col Period
and then used groupby(), ngroup() methods on it to group and assign a unique value to each period and store in Value
column.
Timestamp | Period | Value |
---|---|---|
29-12-2018 | 12-2018 | 1 |
31-12-2018 | 12-2018 | 1 |
01-01-2019 | 01-2019 | 2 |
05-01-2019 | 01-2019 | 2 |
02-02-2018 | 02-2019 | 3 |
P.s I added 1 while creating Value
as the groups were being assigned from 0 but I wanted the groups to start from 1.
答案2
得分: 0
import datetime
timestamps = [datetime.datetime(1984, 1, 2)]
# format - month: value
valuesdict = {1: 1, 2: 2, 3: 3}
values = [valuesdict[i.month] for i in timestamps]
print(values)
这应该适用于您的用例。时间戳必须是datetime.datetime
类型。
英文:
import datetime
timestamps = [datetime.datetime(1984, 1, 2)]
# format - month: value
valuesdict = {1: 1, 2: 2, 3: 3}
values = [valuesdict[i.month] for i in timestamps]
print(values)
This should work for your use case. The timestamps must be of type datetime.datetime
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论