Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Timestamp origin takes no effect in resample for 'MS' frequency #53662

Closed
2 of 3 tasks
MarcoGorelli opened this issue Jun 14, 2023 · 6 comments · Fixed by #53938
Closed
2 of 3 tasks

BUG: Timestamp origin takes no effect in resample for 'MS' frequency #53662

MarcoGorelli opened this issue Jun 14, 2023 · 6 comments · Fixed by #53938
Labels
Bug Resample resample method

Comments

@MarcoGorelli
Copy link
Member

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

df = pd.DataFrame({
    'ts': [dt.datetime(1999, 12, 31, 13, 00)],
    'values': [10.]
})
df.resample('2MS', on='ts', origin=pd.Timestamp('1999-11-01'))['values'].sum()

Issue Description

The code above produces

ts
1999-12-01    10.0
Freq: 2MS, Name: values, dtype: float64

I'm confused though - if origin was set to 1999-11-01, then shouldn't the window be [1999-11-01, 2000-01-01)?

Expected Behavior

ts
1999-11-01 10.0
Freq: 2MS, Name: values, dtype: float64


### Installed Versions

<details>

INSTALLED VERSIONS
------------------
commit           : 965ceca9fd796940050d6fc817707bba1c4f9bff
python           : 3.10.6.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.10.102.1-microsoft-standard-WSL2
Version          : #1 SMP Wed Mar 2 00:30:59 UTC 2022
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_GB.UTF-8
LOCALE           : en_GB.UTF-8

pandas           : 2.0.2
numpy            : 1.24.3
pytz             : 2023.3
dateutil         : 2.8.2
setuptools       : 67.6.1
pip              : 23.0.1
Cython           : None
pytest           : 7.3.1
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 3.1.2
IPython          : 8.13.2
pandas_datareader: None
bs4              : 4.12.2
bottleneck       : None
brotli           : None
fastparquet      : None
fsspec           : None
gcsfs            : None
matplotlib       : 3.7.1
numba            : None
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : 12.0.0
pyreadstat       : None
pyxlsb           : None
s3fs             : None
scipy            : None
snappy           : None
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
zstandard        : None
tzdata           : 2023.3
qtpy             : None
pyqt5            : None


</details>
@MarcoGorelli MarcoGorelli added Bug Needs Triage Issue that has not been reviewed by a pandas team member Resample resample method and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 14, 2023
@mcgeestocks
Copy link
Contributor

Looks interesting; will pick this one up.

@mcgeestocks
Copy link
Contributor

take

@mcgeestocks
Copy link
Contributor

I'm confused though - if origin was set to 1999-11-01, then shouldn't the window be [1999-11-01, 2000-01-01)?

After digging in, I've got a few questions, @MarcoGorelli.

Your comment regarding the window only makes sense to me if you're referring to resample for a period of 2M for months, not 2MS. What's confusing is the title of this Issue references MS for Milliseconds.

It does appear that there is an issue where MS has no impact on resample. This takes place regardless of whether an origin is set or not. Maybe there are two issues here?

See these results:

df = pd.DataFrame({
    'ts': [dt.datetime(1999, 12, 31, 13, 0,0), dt.datetime(1999, 12, 31, 14,0,0)],
    'values': [10., 20.]
})
print(df.resample('2MS', on='ts', origin=pd.Timestamp('1999-11-01'))['values'].sum())

>> ts
>> 1999-12-01    30.0
>> Freq: 2MS, Name: values, dtype: float64

Similarly:

df = pd.DataFrame({
    'ts': [dt.datetime(1999, 12, 31, 13, 0,0), dt.datetime(1999, 12, 31, 14,0,0)],
    'values': [10., 20.]
})
print(df.resample('2MS', on='ts')['values'].sum())

>> ts
>> 1999-12-01    30.0
>> Freq: 2MS, Name: values, dtype: float64

meanwhile running:

df = pd.DataFrame({
    'ts': [dt.datetime(1999, 12, 31, 13, 0,0), dt.datetime(1999, 12, 31, 14,0,0)],
    'values': [10., 20.]
})
print(df.resample('2S', on='ts', origin=pd.Timestamp('1999-11-01'))['values'].sum())
>> ts
>> 1999-12-31 13:00:00    10.0
>> 1999-12-31 13:00:02     0.0
>> 1999-12-31 13:00:04     0.0
>> 1999-12-31 13:00:06     0.0
>> 1999-12-31 13:00:08     0.0
>>                        ... 
>> 1999-12-31 13:59:52     0.0
>> 1999-12-31 13:59:54     0.0
>> 1999-12-31 13:59:56     0.0
>> 1999-12-31 13:59:58     0.0
>> 1999-12-31 14:00:00    20.0 
>> Freq: 2S, Name: values, Length: 1801, dtype: float64

I'm going to focus on why MS has no effect in resample for now. Let me know if I'm misunderstanding.

@MarcoGorelli
Copy link
Member Author

thanks for looking into this

What's confusing is the title of this Issue references MS for Milliseconds.

MS is "month start", https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#dateoffset-objects

And M isn't "month", but "month end". If you think that's confusing - I agree! And there's a big effort going on to change that #52064

@mcgeestocks
Copy link
Contributor

Haha Got it; Your expected output makes a lot more sense, thanks for the context.

@MarcoGorelli
Copy link
Member Author

Noticed while reviewing: here's another test case which needs to pass:

If we have

In [39]: df = pd.DataFrame({'ts': [datetime(1999, 12, 31, 20)], 'values': [10.]})

In [40]: df
Out[40]:
                   ts  values
0 1999-12-31 20:00:00    10.0

and do

df.resample('3YS', on='ts', closed='left', label='left', origin=datetime(1995, 1, 1))['values'].sum()

then I'd expect the windows to be:

  • [1995-01-01, 1998-01-01)
  • [1998-01-01, 2001-01-01)

But instead, I see:

In [42]: df.resample('3YS', on='ts', closed='left', label='left', origin=datetime(1995, 1, 1))['values'].sum()
Out[42]:
ts
1999-01-01    10.0
Freq: 3AS-JAN, Name: values, dtype: float64

@mcgeestocks mcgeestocks removed their assignment Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Resample resample method
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants