python - Pandas cumulative function of series with dates and NaT -


this may known limitation, i'm struggling calculate cumulative minimum of series in pandas when series contains nat's. there way make work?

simple example below:

import pandas pd  s = pd.series(pd.date_range('2008-09-15', periods=10, freq='m')) s.loc[10] = pd.nat s.cummin()  valueerror: not convert object numpy datetime 

this bug has been fixed in pandas 0.15.2 (to released).


as workaround, use skipna=false, , handle nats "manually":

import pandas pd import numpy np np.random.seed(1)  s = pd.series(pd.date_range('2008-09-15', periods=10, freq='m')) s.loc[10] = pd.nat np.random.shuffle(s) print(s) # 0    2008-11-30 # 1    2008-12-31 # 2    2009-01-31 # 3    2009-06-30 # 4    2008-10-31 # 5    2009-03-31 # 6    2008-09-30 # 7    2009-04-30 # 8           nat # 9    2009-05-31 # 10   2009-02-28 # dtype: datetime64[ns]  mask = pd.isnull(s) result = s.cummin(skipna=false) result.loc[mask] = pd.nat print(result) 

yields

0    2008-11-30 1    2008-11-30 2    2008-11-30 3    2008-11-30 4    2008-10-31 5    2008-10-31 6    2008-09-30 7    2008-09-30 8           nat 9    2008-09-30 10   2008-09-30 dtype: datetime64[ns] 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -