Skip to content

Last day of month should group with that month #17898

Open
@louispotok

Description

@louispotok

Code Sample, a copy-pastable example if possible

import pandas as pd
dts = pd.date_range(start='2016-09-01',end='2017-08-31')
df = pd.DataFrame(
    {
        'day': dts, 
        "val": np.random.rand(len(dts)), 
    }
)
right_cls = df.groupby(pd.Grouper(key='day',freq='4M', closed="right")).size()
left_cls = df.groupby(pd.Grouper(key='day',freq='4M',closed="left")).size()

print(right_cls.index.max())
# pd.Timestamp('2017-09-30 00:00:00')
print(left_cls.index.max())
# pd.Timestamp('2017-12-31 00:00:00')

Problem description

I don't know whether this is a bug or whether I don't understand the expected behavior. I would expect the max index value in both cases to be "2017-08-31", but it looks like it's considering 08-31-2017 to be in September. If I have an observation on 2017-08-31 before midnight, it should still be grouped into the period ending 2017-08-31, right?

Expected Output

# This should work
assert right_cls.index.max() == pd.Timestamp('2017-08-31 00:00:00')
assert left_cls.index.max() == pd.Timestamp('2017-08-31 00:00:00')

Output of pd.show_versions()

I'm using pandas 0.20.3 here, but I also checked this on the latest commit and it looks like the behavior persists.

INSTALLED VERSIONS ------------------ commit: None python: 3.6.2.final.0 python-bits: 64 OS: Linux OS-release: 4.10.0-37-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.20.3
pytest: None
pip: 9.0.1
setuptools: 36.4.0
Cython: None
numpy: 1.13.1
scipy: 0.19.1
xarray: None
IPython: 6.1.0
sphinx: None
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.9999999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: 2.7.1 (dt dec pq3 ext lo64)
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions