Skip to content

Center rolling window with date NotImplementedError #20012

Closed
@weber-s

Description

@weber-s

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np

idx = pd.date_range(start="2010-01-01", end="2010-01-30", fred="1D")
df = pd.DataFrame(index=idx, data=np.sin(np.linspace(0,2*np.pi,len(idx))))

# works well
df.rolling(5, center=True, min_periods=5)

# fail with 
# NotImplementedError: center is not implemented for datetimelike and offset based windows
df.rolling("5D", center=True, min_periods=5)

Complete traceback:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-54-058f86b11afc> in <module>()
----> 1 df.rolling("5D", center=True, min_periods=5)

~/.local/lib/python3.5/site-packages/pandas/core/generic.py in rolling(self, window, min_periods, freq, center, win_type, on, axis, closed)
   7063                                    min_periods=min_periods, freq=freq,
   7064                                    center=center, win_type=win_type,
-> 7065                                    on=on, axis=axis, closed=closed)
   7066 
   7067         cls.rolling = rolling

~/.local/lib/python3.5/site-packages/pandas/core/window.py in rolling(obj, win_type, **kwds)
   2064         return Window(obj, win_type=win_type, **kwds)
   2065 
-> 2066     return Rolling(obj, **kwds)
   2067 
   2068 

~/.local/lib/python3.5/site-packages/pandas/core/window.py in __init__(self, obj, window, min_periods, freq, center, win_type, axis, on, closed, **kwargs)
     84         self.win_freq = None
     85         self.axis = obj._get_axis_number(axis) if axis is not None else None
---> 86         self.validate()
     87 
     88     @property

~/.local/lib/python3.5/site-packages/pandas/core/window.py in validate(self)
   1104             # we don't allow center
   1105             if self.center:
-> 1106                 raise NotImplementedError("center is not implemented "
   1107                                           "for datetimelike and offset "
   1108                                           "based windows")

NotImplementedError: center is not implemented for datetimelike and offset based windows

Problem description

The rolling window is not able to be centered with a datetimelike index, which is a problem when dealing with time series.

I think that this is a WIP feature. I don't see anything blocking about it.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.0-3-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: C.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.21.0
pytest: 3.0.6
pip: 9.0.1
setuptools: 38.2.3
Cython: None
numpy: 1.13.3
scipy: 0.19.1
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: None
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: 3.3.0
numexpr: 2.6.1
feather: None
matplotlib: 2.0.0
openpyxl: 2.3.0
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: 3.7.1
bs4: 4.5.3
html5lib: 0.999999999
sqlalchemy: 1.2.2
pymysql: None
psycopg2: 2.6.2 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions