英文:
Dataframe cannot delete nan
问题
我有一个从.csv文件创建的数据框。数据框具有DatetimeIndex和一个包含股票价格(float64)的列。在创建数据框时,我将频率设置为'D',现在周末的地方有nan条目。
我尝试了dropna(),但每次检查前10行时,nan仍然存在。
如果我在创建数据框时不使用freq()方法,问题就解决了,但是出于建模目的,我需要索引具有该频率。
我是否漏掉了或不理解了什么?
data = pd.read_csv(r'C:/Users/Oliver/Documents/Data/EURUSD.csv', index_col='Date', parse_dates=True).asfreq('D')
data.drop(columns=['Time', 'Open', 'High', 'Low', 'Volume'], inplace=True)
data.dropna(how='any', axis=0)
data.head(10)
Close
Date
2003-05-06 1.14338
2003-05-07 1.13647
2003-05-08 1.14996
2003-05-09 1.14877
2003-05-10 NaN
2003-05-11 NaN
2003-05-12 1.15427
2003-05-13 1.15120
2003-05-14 1.14940
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?
data = pd.read_csv(r'C:/Users/Oliver/Documents/Data/EURUSD.csv', index_col='Date', parse_dates=True, ).asfreq('D')
data.drop(columns=['Time', 'Open', 'High', 'Low', 'Volume'], inplace=True)
data.dropna(how='any', axis=0)
data.head(10)
Close
Date
2003-05-06 1.14338
2003-05-07 1.13647
2003-05-08 1.14996
2003-05-09 1.14877
2003-05-10 NaN
2003-05-11 NaN
2003-05-12 1.15427
2003-05-13 1.15120
2003-05-14 1.14940
2003-05-15 1.13847
答案1
得分: 0
问题在于,如果没有明确指定,你的dropna
不会是一个inplace
操作。请尝试以下替代你第三行代码中使用.dropna
的方式之一:
# 指定 inplace=True 参数
data.dropna(how='any', axis=0, inplace=True)
或者
# 在操作后覆盖原始数据框
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
-
#Specify inplace=True parameter
data.dropna(how='any', axis=0, inplace=True)
Or
#Overwrite original dataframe after the operation
data = data.dropna(how='any', axis=0)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论