Dataframe 无法删除 NaN。

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

Dataframe cannot delete nan

问题

我有一个从.csv文件创建的数据框。数据框具有DatetimeIndex和一个包含股票价格(float64)的列。在创建数据框时,我将频率设置为'D',现在周末的地方有nan条目。

我尝试了dropna(),但每次检查前10行时,nan仍然存在。

如果我在创建数据框时不使用freq()方法,问题就解决了,但是出于建模目的,我需要索引具有该频率。

我是否漏掉了或不理解了什么?

  1. data = pd.read_csv(r'C:/Users/Oliver/Documents/Data/EURUSD.csv', index_col='Date', parse_dates=True).asfreq('D')
  2. data.drop(columns=['Time', 'Open', 'High', 'Low', 'Volume'], inplace=True)
  3. data.dropna(how='any', axis=0)
  4. data.head(10)
  5. Close
  6. Date
  7. 2003-05-06 1.14338
  8. 2003-05-07 1.13647
  9. 2003-05-08 1.14996
  10. 2003-05-09 1.14877
  11. 2003-05-10 NaN
  12. 2003-05-11 NaN
  13. 2003-05-12 1.15427
  14. 2003-05-13 1.15120
  15. 2003-05-14 1.14940
  16. 2003-05-15 1.13847
英文:

Ive a dataframe I created from a .csv. The Dataframe has a DatetimeIndex and and one column containing stock prices(float64). When creating the dataframe I set frequency to 'D' and now i have nan entries for weekends.

ive tried dropna() but everytime i check with head(10) the nan's remain.

if I don't use the freq() method when creating dataframe it solves the problem but I need the the Index to have said frequency for modelling purposes.

if there something im missing/not understanding?

  1. data = pd.read_csv(r'C:/Users/Oliver/Documents/Data/EURUSD.csv', index_col='Date', parse_dates=True, ).asfreq('D')
  2. data.drop(columns=['Time', 'Open', 'High', 'Low', 'Volume'], inplace=True)
  3. data.dropna(how='any', axis=0)
  4. data.head(10)
  5. Close
  6. Date
  7. 2003-05-06 1.14338
  8. 2003-05-07 1.13647
  9. 2003-05-08 1.14996
  10. 2003-05-09 1.14877
  11. 2003-05-10 NaN
  12. 2003-05-11 NaN
  13. 2003-05-12 1.15427
  14. 2003-05-13 1.15120
  15. 2003-05-14 1.14940
  16. 2003-05-15 1.13847

答案1

得分: 0

问题在于,如果没有明确指定,你的dropna不会是一个inplace操作。请尝试以下替代你第三行代码中使用.dropna的方式之一:

  1. # 指定 inplace=True 参数
  2. data.dropna(how='any', axis=0, inplace=True)

或者

  1. # 在操作后覆盖原始数据框
  2. data = data.dropna(how='any', axis=0)
英文:

The issue here is that your dropna is not an inplace operation unless explicitly specified. Try this instead of your third line of code where you are using .dropna -

  1. #Specify inplace=True parameter
  2. data.dropna(how='any', axis=0, inplace=True)

Or

  1. #Overwrite original dataframe after the operation
  2. data = data.dropna(how='any', axis=0)

huangapple
  • 本文由 发表于 2023年1月9日 07:41:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052075.html
匿名

发表评论

匿名网友

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

确定