从日期数组中删除日期

问题描述 投票:-1回答:2

我创建了一个从2014-12-12014-12-31的日期数组,但我需要从中删除2014-12-29条目。我怎么做 ?

python pandas
2个回答
1
投票

使用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')))

0
投票

那天使用布尔检查

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