question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: Date offset on numpy datetime64 does not work for week and month offsets

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample

import numpy as np
import pandas as pd

# test data: a numpy Datetime64
ts_np = np.Datetime64('2021-01-01T08:00:00.00')

# 1 hour offset: works as expected (1 day offset also works as expected)
do=pd.tseries.frequencies.to_offset('1h')
ts_np + do

Result

Timestamp('2021-01-01 09:00:00')
# 1 week offset returns an error
do=pd.tseries.frequencies.to_offset('1W')
ts_np + do

Result

ts_np+do
Traceback (most recent call last):

  File "<ipython-input-11-e20c02eb9ea8>", line 1, in <module>
    ts_np+do

UFuncTypeError: ufunc 'add' cannot use operands with types dtype('<M8[ms]') and dtype('O')
# 1 month begin offset returns an error
do=pd.tseries.frequencies.to_offset('1MS')

Result

ts_np+do
Traceback (most recent call last):

  File "<ipython-input-13-e20c02eb9ea8>", line 1, in <module>
    ts_np+do

UFuncTypeError: ufunc 'add' cannot use operands with types dtype('<M8[ms]') and dtype('O')

Problem description

Applying week or month offset on numpy Datetime64 does not work.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2cb96529396d93b46abab7bbc73a208e708c642e python : 3.8.5.final.0 python-bits : 64 OS : Linux OS-release : 5.8.0-53-generic Version : #60~20.04.1-Ubuntu SMP Thu May 6 09:52:46 UTC 2021 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : fr_FR.UTF-8 LOCALE : fr_FR.UTF-8

pandas : 1.2.4 numpy : 1.20.1 pytz : 2021.1 dateutil : 2.8.1 pip : 21.1.1 setuptools : 52.0.0.post20210125 Cython : 0.29.23 pytest : 6.2.3 hypothesis : None sphinx : 4.0.1 blosc : None feather : None xlsxwriter : 1.3.8 lxml.etree : 4.6.3 html5lib : 1.1 pymysql : None psycopg2 : None jinja2 : 3.0.0 IPython : 7.22.0 pandas_datareader: None bs4 : 4.9.3 bottleneck : 1.3.2 fsspec : 0.9.0 fastparquet : 0.6.0 gcsfs : None matplotlib : 3.3.4 numexpr : 2.7.3 odfpy : None openpyxl : 3.0.7 pandas_gbq : None pyarrow : 2.0.0 pyxlsb : None s3fs : None scipy : 1.6.2 sqlalchemy : 1.4.15 tables : 3.6.1 tabulate : None xarray : None xlrd : 2.0.1 xlwt : 1.3.0 numba : 0.51.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jbrockmendelcommented, Jan 3, 2022

do here is a Week offset with weekday=6, so ts_np + do is giving you the next date with res.weekday() ==6. I think you want either to_offset("7D") or pd.offsets.Week(n=1, weekday=None)

1reaction
ghostcommented, May 27, 2021

A possible workaround to this can be to use the timedelta64 function of numpy:

import numpy as np
import pandas as pd

ts_np = np.Datetime64('2021-01-01T08:00:00.00')
do = np.timedelta64(1, 'W')
ts_np + do

Output:

2021-01-08T08:00:00.000
Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy and pandas timedelta error - python - Stack Overflow
using pandas, and I want to add one year to each date. I can get it working using pandas but not using numpy....
Read more >
Datetimes and Timedeltas — NumPy v1.24 Manual
The “busday” functions can additionally check a list of “holiday” dates, specific dates that are not valid days. The function busday_offset allows you...
Read more >
Datetimes and Timedeltas — NumPy v1.8 Manual
The “busday” functions can additionally check a list of “holiday” dates, specific dates that are not valid days. The function busday_offset allows you...
Read more >
Using Python datetime to Work With Dates and Times
The database contains a copy of all the designated time zones and how many hours and minutes they're offset from UTC. So, during...
Read more >
What's New — pandas 0.23.4 documentation - PyData |
With NumPy 1.15 and pandas 0.23.1 or earlier, numpy.all() will no longer reduce over ... Do not coerce the datetime.date >>> Series(pd.date_range('2017', ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found