Home > Enterprise >  Dataframe cannot delete nan
Dataframe cannot delete nan

Time:01-09

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

CodePudding user response:

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)
  • Related