drop datetimeindex其中df表示nan

时间:2016-10-07 01:54:15

标签: python pandas dataframe

我有一个带有日期时间索引的pandas数据框,在某些行上有nan值。如何删除datetimeindex以及nan行?

    <div class = "row Date" ng-switch="myVar">
    <div class = "col-lg-1"></div>
    <div class = "col-lg-6" ng-switch-default>
          <md-input-container>
            <label>Depart on</label>
            <md-datepicker ng-model="user.submissionDate"></md-datepicker>
          </md-input-container>
    </div>
    <div class = "col-lg-10" ng-switch-when="RoundTrip">
          <md-input-container>
            <label>Depart on</label>
            <md-datepicker ng-model="user.submissionDate"></md-datepicker>
          </md-input-container>

          <md-input-container>
            <label>Return on</label>
            <md-datepicker ng-model="user.submissionReturnDate"></md-datepicker>
          </md-input-container>
    </div>
    </div>

我试过了:

2016-10-06 13:15:00  2.923383  0.007970 -0.001883
2016-10-06 13:30:00  2.809612  0.007389  0.001466
2016-10-06 13:45:00  3.022803  0.028234 -0.005162
2016-10-06 14:00:00  3.005836  0.017393 -0.000727
2016-10-06 14:15:00  3.031413  0.002826 -0.001097
2016-10-06 14:30:00  3.107922  0.011489  0.001837
2016-10-06 14:45:00  3.090017 -0.015071  0.006606
2016-10-06 15:00:00  3.032213 -0.028361 -0.008619
2016-10-06 15:15:00  3.010773 -0.020547  0.008827
2016-10-06 15:30:00  2.948293 -0.002611  0.013339
2016-10-06 15:45:00  2.965507 -0.012090  0.004819
2016-10-06 16:00:00  2.939935  0.009255 -0.016812
2016-10-06 16:15:00       NaN       NaN       NaN
2016-10-06 16:30:00       NaN       NaN       NaN
2016-10-06 16:45:00       NaN       NaN       NaN
2016-10-06 17:00:00       NaN       NaN       NaN
2016-10-06 17:15:00       NaN       NaN       NaN
2016-10-06 17:30:00       NaN       NaN       NaN
2016-10-06 17:45:00       NaN       NaN       NaN
2016-10-06 18:00:00  2.790215 -0.006258 -0.006561
2016-10-06 18:15:00  2.760398 -0.019173 -0.005650
2016-10-06 18:30:00  2.806837 -0.004759  0.003778
2016-10-06 18:45:00  2.707243 -0.011007  0.000657
2016-10-06 19:00:00  2.690583 -0.011315  0.011752
2016-10-06 19:15:00  2.632939 -0.010978  0.018907
2016-10-06 19:30:00  2.665248 -0.009146  0.016380
2016-10-06 19:45:00  2.637122 -0.015417  0.021086
2016-10-06 20:00:00  2.688877 -0.004790  0.009998
2016-10-06 20:15:00  2.574410 -0.000862  0.014240
2016-10-06 20:30:00  2.641405  0.010043  0.010205

但是返回的是上面的内容。

请注意,for row in range(len(df)): if df.iloc[row,:] is None: df.index.drop(row) 不是我正在寻找的...我正在处理在图上完成的缺少时间序列数据插值。我希望连接丢失的数据,因此我没有间隙(由NaN引起)或插值(由数据缺失引起但数据时间索引仍在那里)在图表上。我不想进一步深入研究,但显然我已被告知要删除NaN行 datetimeindex,其中出现的NaN行是一个解决方案......

1 个答案:

答案 0 :(得分:0)

如果你只是想找到连续没有 NaN的索引,你可以这样做:

# get a single column of T/F values that 
# where at least one value in the row is null
msk = df.isnull().any(axis=1)

# now drop all of the index values where msk = True
new_index = df.index.drop(msk[msk == True].index)

但是,new_index只是索引值的列表,其中没有列是NaN。这样做不会修改DataFrame实例。

您也可以使用interpolate().

自行插入数据

您可能希望执行以下操作:

df.interpolate(method="time")