如何为时间序列数据按月分配值?

huangapple go评论102阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月19日 13:03:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定