如何将Period字符串转换为实际的Period类型(How to convert Period string to actual Period type)

系统教程 行业动态 更新时间:2024-06-14 16:53:13
如何将Period字符串转换为实际的Period类型(How to convert Period string to actual Period type)

我有一列数据,如'1971q1' , '1972q2'等(年份后跟季度)当我这样做时:

print(type(df.Quarterly))

答案是Series

我需要的是“转换”/将此列转换为真正的pd.Period类型,以便我可以使用它进行简单的时间代数。 谢谢

I have a column of data such as '1971q1', '1972q2', etc. (year followed by quarter) When I do:

print(type(df.Quarterly))

the answer is Series

What I need is to "cast"/convert this column to a genuine pd.Period type so I can do simple time algebra with it. Thank you

最满意答案

你可以使用pd.PeriodIndex()方法。

假设您有以下DF:

In [517]: x Out[517]: str_col 0 1971q1 1 1971q2 2 1971q3 3 1971q4 4 1972q1 5 1972q2 6 1972q3 7 1972q4 In [518]: x.dtypes Out[518]: str_col object dtype: object

让我们创建一个新的“期间”列:

In [519]: x['period'] = pd.PeriodIndex(x.str_col, freq='Q') In [520]: x Out[520]: str_col period 0 1971q1 1971Q1 1 1971q2 1971Q2 2 1971q3 1971Q3 3 1971q4 1971Q4 4 1972q1 1972Q1 5 1972q2 1972Q2 6 1972q3 1972Q3 7 1972q4 1972Q4 In [521]: x.dtypes Out[521]: str_col object period object dtype: object

现在我们可以做“时间代数”,例如让我们从每个时期减去四分之一:

In [525]: x.period - 1 Out[525]: 0 1970Q4 1 1971Q1 2 1971Q2 3 1971Q3 4 1971Q4 5 1972Q1 6 1972Q2 7 1972Q3 Name: period, dtype: object

或者,您可以将str_col列str_col为常规Pandas / NumPy datetime :

In [527]: pd.to_datetime(x.str_col, errors='coerce') Out[527]: 0 1971-01-01 1 1971-04-01 2 1971-07-01 3 1971-10-01 4 1972-01-01 5 1972-04-01 6 1972-07-01 7 1972-10-01 Name: str_col, dtype: datetime64[ns]

you can use pd.PeriodIndex() method.

Assume you have the following DF:

In [517]: x Out[517]: str_col 0 1971q1 1 1971q2 2 1971q3 3 1971q4 4 1972q1 5 1972q2 6 1972q3 7 1972q4 In [518]: x.dtypes Out[518]: str_col object dtype: object

let's create a new 'period' column:

In [519]: x['period'] = pd.PeriodIndex(x.str_col, freq='Q') In [520]: x Out[520]: str_col period 0 1971q1 1971Q1 1 1971q2 1971Q2 2 1971q3 1971Q3 3 1971q4 1971Q4 4 1972q1 1972Q1 5 1972q2 1972Q2 6 1972q3 1972Q3 7 1972q4 1972Q4 In [521]: x.dtypes Out[521]: str_col object period object dtype: object

now we can do "time algebra", for example let's subtract one quarter from each period:

In [525]: x.period - 1 Out[525]: 0 1970Q4 1 1971Q1 2 1971Q2 3 1971Q3 4 1971Q4 5 1972Q1 6 1972Q2 7 1972Q3 Name: period, dtype: object

alternatively you can cast the str_col column to regular Pandas/NumPy datetime:

In [527]: pd.to_datetime(x.str_col, errors='coerce') Out[527]: 0 1971-01-01 1 1971-04-01 2 1971-07-01 3 1971-10-01 4 1972-01-01 5 1972-04-01 6 1972-07-01 7 1972-10-01 Name: str_col, dtype: datetime64[ns]

更多推荐

本文发布于:2023-04-06 01:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/8915ca988371d575de5e466638a7933c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   转换为   如何将   类型   Period

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!