-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
Code Sample, a copy-pastable example if possible
df = pd.DataFrame.from_records({'ref':['a','a','a','b','b'],
'time':[dt.datetime(2014,12,31), dt.datetime(2015,12,31), dt.datetime(2016,12,31),
dt.datetime(2012,12,31), dt.datetime(2014,12,31)],
'value':5*[1]})
# These frames differs
pd.DataFrame.equals(df.groupby("ref").resample(rule='M', on='time')['value'].sum(),
df.set_index('time').groupby("ref").resample(rule='M')['value'].sum())
# Similarly to .set_index() .apply produces the correct output
pd.DataFrame.equals(df.groupby("ref").apply(lambda f :f.resample(rule='M', on='time')['value'].sum()),
df.set_index('time').groupby("ref").resample(rule='M')['value'].sum())
#This is the incorrect frame
df.groupby("ref").resample(rule='5T', on='time')['value'].sum().to_frame()
Problem description
DataFrameGroupby.resample with the on
keyword (last line) produces an incorrect output: it differs from the output produce with a DateTimeIndex and has incorrect values. It seems that it does not handle the group properly:
- the result has an entry for
('a', 2012-12-31 00:00:00):1
that is incorrectref =='a'
only starts in 2014 so not only should there be no entry but value of 1 is incorrect - the result has entries for 'b' starting in 2015 when
ref == 'b'
has a min time entry in 2015 so there should not be any entry before that regardless of its value
Expected Output
True
for pd.DataFrame.equals(df.groupby("ref").resample(rule='M', on='time')['value'].sum(), df.set_index('time').groupby("ref").resample(rule='M')['value'].sum())
Output of pd.show_versions()
[paste the output of pd.show_versions()
here below this line]
INSTALLED VERSIONS
commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Darwin
OS-release: 18.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_CA.UTF-8
LOCALE: en_CA.UTF-8
pandas: 0.24.2
pytest: None
pip: 19.1.1
setuptools: 41.0.1
Cython: None
numpy: 1.16.4
scipy: None
pyarrow: None
xarray: None
IPython: 7.6.1
sphinx: None
patsy: None
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None