python - First Value in Pandas DatetimeIndex is Unsearchable -
i have simple series:
>>> sub_dim_metrics date 2017-04-04 00:00:00+00:00 32.38 2017-04-03 00:00:00+00:00 246.28 2017-04-02 00:00:00+00:00 146.25 2017-04-01 00:00:00+00:00 201.98 2017-03-31 00:00:00+00:00 274.74 2017-03-30 00:00:00+00:00 257.82 2017-03-29 00:00:00+00:00 279.38 2017-03-28 00:00:00+00:00 203.53 2017-03-27 00:00:00+00:00 250.65 2017-03-26 00:00:00+00:00 180.59 2017-03-25 00:00:00+00:00 196.61 2017-03-24 00:00:00+00:00 281.04 2017-03-23 00:00:00+00:00 276.44 2017-03-22 00:00:00+00:00 227.55 2017-03-21 00:00:00+00:00 267.59 name: area, dtype: float64 >>> sub_dim_metrics.index datetimeindex(['2017-04-04', '2017-04-03', '2017-04-02', '2017-04-01', '2017-03-31', '2017-03-30', '2017-03-29', '2017-03-28', '2017-03-27', '2017-03-26', '2017-03-25', '2017-03-24', '2017-03-23', '2017-03-22', '2017-03-21'], dtype='datetime64[ns, utc]', name=u'date', freq=none) later in code retrieve area specific days using following format: sub_dim_metrics['2017-04-02'], example.
before retrieve area day, first verify requested date in series, so: if '2017-04-02' in sub_dim_metrics.index
my problem first value in index not return true, while rest do:
>>> '2017-04-02' in sub_dim_metrics.index true >>> '2017-04-04' in sub_dim_metrics.index false why , best way verify date in series before retrieving corresponding value?
iiuc:
you getting false when expect true:
checking whether string in datetime index. apparently pandas loose check , tries you. it's getting wrong though, isn't it.
plan 1
right!
pd.to_datetime('2017-04-04') in sub_dim_metrics.index true plan 2
think unsorted-ness throwing off. sort_values first.
'2017-04-04' in sub_dim_metrics.index.sort_values() true setup
from io import stringio import pandas pd txt = """2017-04-04 00:00:00+00:00 32.38 2017-04-03 00:00:00+00:00 246.28 2017-04-02 00:00:00+00:00 146.25 2017-04-01 00:00:00+00:00 201.98 2017-03-31 00:00:00+00:00 274.74 2017-03-30 00:00:00+00:00 257.82 2017-03-29 00:00:00+00:00 279.38 2017-03-28 00:00:00+00:00 203.53 2017-03-27 00:00:00+00:00 250.65 2017-03-26 00:00:00+00:00 180.59 2017-03-25 00:00:00+00:00 196.61 2017-03-24 00:00:00+00:00 281.04 2017-03-23 00:00:00+00:00 276.44 2017-03-22 00:00:00+00:00 227.55 2017-03-21 00:00:00+00:00 267.59""" sub_dim_metrics = pd.read_csv(stringio(txt), sep='\s{2,}', engine='python', index_col=0, parse_dates=[0], header=none, names=['date', 'area'], squeeze=true)
Comments
Post a Comment