pandas DataFrame 计算每周协方差

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

pandas daframe compute covarince weekly

问题

我有这个数据框:

  1. np.random.seed(0)
  2. start_d = '2018-01-01 00:00:00'
  3. start_d = pd.to_datetime(start_d,format='%Y-%m-%d %H:%M:%S')
  4. end_d = '2018-01-28 00:00:00'
  5. end_d = pd.to_datetime(end_d,format='%Y-%m-%d %H:%M:%S')
  6. index = pd.date_range(start = start_d, end = end_d)
  7. df = pd.DataFrame(index=index,data=np.random.randint(0,100,size=(28, 2)), columns=list('AB'))

我想计算两个序列之间的相关性,但基于每周。换句话说,我考虑一种特定应用的重新采样。我的想法是同时应用Pearson和Spearman。为了表达清楚:

  1. df.resample('W').corr(method='spearman)

你觉得呢?能做类似的事情吗?

祝好。

英文:

I have this dataframe

  1. np.random.seed(0)
  2. start_d = '2018-01-01 00:00:00'
  3. start_d = pd.to_datetime(start_d,format='%Y-%m-%d %H:%M:%S')
  4. end_d = '2018-01-28 00:00:00'
  5. end_d = pd.to_datetime(end_d,format='%Y-%m-%d %H:%M:%S')
  6. index = pd.date_range(start = start_d, end = end_d)
  7. df = pd.DataFrame(index=index,data=np.random.randint(0,100,size=(28, 2)), columns=list('AB'))

I would like to compute the correlation between the two series but on a weekly base. In other words, I am thinking about a sort of resample with a specific apply. My idea is to apply both Pearson and Spearman. To make myself clear:

  1. df.resample('W').corr(method='spearman)

What do you think? Is it possible to do something similar?

Best.

答案1

得分: 1

如果我理解这个问题正确,你试图在每周的水平上获得相关性。是否有多年的日期?

如果只有一年:

  1. # 设置周数:
  2. df['Week_Number'] = df['Date'].dt.isocalendar().week
  3. # 现在按周数分组,并获得相关性:
  4. df.groupby('Week_Number')[['A', 'B']].corr()

如果有超过1年:

  1. # 设置周数和年份:
  2. df['Week_Number'] = df['Date'].dt.isocalendar().week
  3. df['Year'] = df['Date'].dt.year
  4. # 现在按周数和年份分组,并获得相关性:
  5. df.groupby(['Week_Number', 'Year'])[['A', 'B']].corr()
英文:

If I understand this question correctly, you're trying to get the correlation at a week level. Are there multiple years of dates?

If you only have one year:

  1. # Set the week number:
  2. df['Week_Number'] = df['Date'].dt.isocalendar().week
  3. # Now groupby the week number, and get the correlation:
  4. df.groupby('Week_Number')[['A', 'B']].corr()

If you have >1 year:

  1. # Set both the week and year:
  2. df['Week_Number'] = df['Date'].dt.isocalendar().week
  3. df['Year'] = df['Date'].dt.year
  4. # Now groupby the week number and year, and get the correlation:
  5. df.groupby(['Week_Number', 'Year'])[['A', 'B']].corr()

huangapple
  • 本文由 发表于 2023年3月20日 23:00:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75791931.html
匿名

发表评论

匿名网友

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

确定