【问题标题】:Percentage change on a time series in pandaspandas 时间序列的百分比变化
【发布时间】:2020-10-19 14:07:44
【问题描述】:

我是 python 新手,通过一些基本的股票数据分析来学习它。以下是我正在使用的数据框

                      date      open      high  ...       close  volume 
0      2010-01-05 09:16:00   5282.00   5283.10  ...   5281.10  94700    NaN
1      2010-01-05 12:16:00   5281.60   5281.60  ...   5278.30  49100    NaN
2      2010-01-05 16:16:00   5278.50   5280.50  ...   5278.80  62550    NaN
3      2010-01-06 09:16:00   5278.80   5279.45  ...   5277.30  64850    NaN
4      2010-01-06 12:16:00   5277.95   5278.00  ...   5276.00  65251    NaN

如您所见,它是一个时间序列,一天内有不同的时间段。所以我想找到2010-01-06 09:16:00 的prtc_change(百分比变化)打开与2010-01-05 16:16:00 的关闭相比。 如何计算?

这是我正在寻找的输出类型:

                      date      open      high  ...       close  volume %change
0      2010-01-05 09:16:00   5282.00   5283.10  ...   5281.10  94700    
1      2010-01-05 12:16:00   5281.60   5281.60  ...   5278.30  49100    
2      2010-01-05 16:16:00   5278.50   5280.50  ...   5278.80*  62550    
3      2010-01-06 09:16:00   5278.80*   5279.45  ...   5277.30  64850    0
4      2010-01-06 12:16:00   5277.95   5278.00  ...   5276.00  65251    

%change 列的 2010-01-05-close 到 2010-01-05 9:16-open 的值为 0,因为 open = close (5278.80 == 5278.80)(由 * 标记)。

注意:我在处理数据时对数据进行了一些操作。以下是代码

import pandas as pd
import datetime

df = pd.read_csv(r'C:\Users\Admin\Desktop\Python files\nifty.txt' , sep = ';' , names = ["dates","open","high","low","close","volume"])
## fomration the date and time
df['dates'] = pd.to_datetime(df['dates'].astype(str) , format='%Y%m%d %H%M%S' )
## splitting the datetime column into date and time
df['date'] = [d.date() for d in df['dates']]
df['time'] = [d.time() for d in df['dates']]

当前数据框如下所示:

                     dates      open      high  ...  volume        date      time
0      2010-01-05 09:16:00   5282.00   5283.10  ...   94700  2010-01-05  09:16:00
1      2010-01-05 12:16:00   5281.60   5281.60  ...   49100  2010-01-05  12:16:00
2      2010-01-05 16:16:00   5278.50   5280.50  ...   62550  2010-01-05  16:16:00
3      2010-01-06 09:16:00   5278.80   5279.45  ...   64850  2010-01-05  09:16:00
4      2010-01-06 12:16:00   5277.95   5278.00  ...   65251  2010-01-05  12:16:00

【问题讨论】:

  • 您好,欢迎来到 stackoverflow。你的意思是你想比较最早的“开盘”和最晚的“收盘”吗?最好将预期的输出添加到您的问题中,以便于理解您的问题。
  • 嗯,我想得到一天最近收盘价和第二天最早开盘价的百分比变化。抱歉,请用需要什么样的输出来编辑我的问题
  • @Fudgster 日期是否总是在您的数据框中排序?
  • yes shubham 它的股票数据,所以它总是排序的
  • @Fudgster 那么替代解决方案应该是可能的。

标签: python-3.x pandas time-series


【解决方案1】:

Pandaspct_change 函数,但它计算百分比变化 在源 Series 的连续元素之间,或对于 源 DataFrame 中的数字类型。

所以在你的情况下它是没用的,你需要一种不同的方法:

  1. 第一步是找到每天的第一个开盘价和最后一个收盘价:

     days = df.groupby(df.date.dt.date).agg({'open': 'first', 'close': 'last'})
    
  2. 然后,计算百分比变化:

     100 * (days.open - days.close.shift()) / days.open
    

详情:

  • days.open - 当天最早的开放时间。
  • days.close.shift() - 前一天的最新收盘价。
  • 100 * ... - 将结果表示为百分比。

第二步是将这些数据与原始DataFrame“连接”起来 (创建一个新列):

  1. 为特定日期的一组行定义一个计算 %change 列的函数:

     def pctChg(grp):
         rv = pd.Series('', index=grp.index)
         chg = days.pct.asof(grp.iloc[0, 0])
         if pd.notnull(chg): rv.iloc[0] = chg
         return rv
    
  2. 然后创建新列:

     df['%change'] = df.groupby(df.date.dt.date)\
         .apply(pctChg).reset_index(level=0, drop=True)
    

【讨论】:

  • 我认为你误解了 OP 的问题......他想要上一个之间的百分比变化。当天收盘和当天开盘...
  • 谢谢,正如 Yatin 上面所说,逻辑有点不对劲。但感谢您的方法。可以使用这种方法。我可以确定接近第二天开放的日子,然后计算百分比。
  • @vladi ,我绑定了代码以获得数据框中最早的打开和最早的关闭。但最终出现错误。说 AttributeError: 'DatetimeProperties' 对象没有属性 'dates' 。我已经编辑了我的问题,因为我稍微操纵了 dta。也为他们发布代码
  • 编写我的代码时,您只有 date 列,正如我所假设的那样,datetime 类型(+ 一些 float i> 列)。现在您已经更改了源数据结构,所以我的代码可能会失败。改变假设然后要求对初始解决方案进行更正是一个坏习惯。这是另一个问题的材料。
  • sorry vlaid,我把数据框改回来了,但奇怪的是遇到了同样的问题。下一篇文章开始。请确保不要更改格式。非常感谢
【解决方案2】:

用途:

df['dates'] = pd.to_datetime(df['dates'])
close = df['close'].shift()
df['% change'] = np.where(
    df['date'].dt.day.diff().gt(0), ((df['open'] - close) / close) * 100, '')

结果:

# print(df)
                 date     open     high   close  volume % change
0 2010-01-05 09:16:00  5282.00  5283.10  5281.1   94700         
1 2010-01-05 12:16:00  5281.60  5281.60  5278.3   49100         
2 2010-01-05 16:16:00  5278.50  5280.50  5278.8   62550         
3 2010-01-06 09:16:00  5278.80  5279.45  5277.3   64850      0.0
4 2010-01-06 12:16:00  5277.95  5278.00  5276.0   65251         

【讨论】:

  • 嗨,Shubham,感谢您的回复,这是为了找到每个时间段的 %change。我只看天变的时候。例如:2010-01-05 16:16:00 是最后一个时间段,因此使用该时间段的收盘价和第二天的开盘价。
猜你喜欢
  • 2019-01-26
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多