Can pandas.DatetimeIndex remember whether it is closed?
我有一个
1 2 3 4 5 6 7 | >>> index = pd.DatetimeIndex(start='2018-01-01', end='2018-01-04', freq='1D', closed='left') DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03'], dtype='datetime64[ns]', freq='D') |
如何再次获得正确的open
这是不可能的,因为在构造对象之后,这些信息会丢失。在创建时,间隔将展开为结果序列:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class DatetimeIndex(<...>): <...> @classmethod def _generate(cls, start, end, periods, name, freq, tz=None, normalize=False, ambiguous='raise', closed=None): <...> index = tools.to_datetime(np.linspace(start.value, end.value, periods), utc=True) <...> if not left_closed and len(index) and index[0] == start: index = index[1:] if not right_closed and len(index) and index[-1] == end: index = index[:-1] index = cls._simple_new(index, name=name, freq=freq, tz=tz) return index |
您可以将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import inspect, collections class SiDatetimeIndex(pd.DatetimeIndex): _Interval = collections.namedtuple('Interval', ('start','end','freq','closed')) #add 'interval' to dir(): DatetimeIndex inherits pandas.core.accessor.DirNamesMixin _accessors = pd.DatetimeIndex._accessors | frozenset(('interval',)) def __new__(cls, *args, **kwargs): base_new = super(SiDatetimeIndex,cls).__new__ callargs = inspect.getcallargs(base_new,cls,*args,**kwargs) result = base_new(**callargs) result.interval = cls._Interval._make(callargs[a] for a in cls._Interval._fields) return result In [31]: index = SiDatetimeIndex(start='2018-01-01', ...: end='2018-01-04', ...: freq='1D', ...: closed='left') In [38]: index.interval Out[38]: Interval(start='2018-01-01', end='2018-01-04', freq='1D', closed='left') |
号
不要期望所有的
这种东西对你有用吗?
1 2 3 4 5 6 7 | index = pd.DatetimeIndex(start='2018-01-01', end='2018-01-04', freq='1D', closed='left') def get_end(index, freq): if freq == '1D': return(index.max()+1) get_end(index, '1D') |
您可以为1d/2d/1M编写逻辑。此外,还可以将带有freq参数的dateindex的列名作为后缀/前缀'purchase_date_1d',如果您甚至不想将其作为单独的输入进行分析。