从日期数组中删除日期

时间:2019-04-30 08:51:56

标签: python pandas

我从datetime2014-12-1创建了2014-12-31个对象的数组,但是我需要从其中删除2014-12-29条目。我该怎么办?

2 个答案:

答案 0 :(得分:2)

使用boolean indexingIndex.drop

data = pd.date_range('2014-12-1', '2014-12-31')
print (data)
DatetimeIndex(['2014-12-01', '2014-12-02', '2014-12-03', '2014-12-04',
               '2014-12-05', '2014-12-06', '2014-12-07', '2014-12-08',
               '2014-12-09', '2014-12-10', '2014-12-11', '2014-12-12',
               '2014-12-13', '2014-12-14', '2014-12-15', '2014-12-16',
               '2014-12-17', '2014-12-18', '2014-12-19', '2014-12-20',
               '2014-12-21', '2014-12-22', '2014-12-23', '2014-12-24',
               '2014-12-25', '2014-12-26', '2014-12-27', '2014-12-28',
               '2014-12-29', '2014-12-30', '2014-12-31'],
              dtype='datetime64[ns]', freq='D')

print (data[data != '2014-12-29'])
DatetimeIndex(['2014-12-01', '2014-12-02', '2014-12-03', '2014-12-04',
               '2014-12-05', '2014-12-06', '2014-12-07', '2014-12-08',
               '2014-12-09', '2014-12-10', '2014-12-11', '2014-12-12',
               '2014-12-13', '2014-12-14', '2014-12-15', '2014-12-16',
               '2014-12-17', '2014-12-18', '2014-12-19', '2014-12-20',
               '2014-12-21', '2014-12-22', '2014-12-23', '2014-12-24',
               '2014-12-25', '2014-12-26', '2014-12-27', '2014-12-28',
               '2014-12-30', '2014-12-31'],
              dtype='datetime64[ns]', freq=None)

或者:

print (data.drop(pd.Timestamp('2014-12-29')))

答案 1 :(得分:0)

在当天使用布尔值检查

df = df[df['date'] !='2014-12-29']