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

Handling of pandas datetimes in numpy #12936

Open
hjstein opened this issue Feb 5, 2019 · 1 comment
Open

Handling of pandas datetimes in numpy #12936

hjstein opened this issue Feb 5, 2019 · 1 comment

Comments

@hjstein
Copy link

hjstein commented Feb 5, 2019

I'm finding a regression from numpy v1.14.3 to numpy v1.15.4. The issue is that in v1.14.3, busday_count() would operate on datetime64 types, and in v1.15.4, this is no longer the case. This comes up when trying to use busday_count() on dates from a pandas dataframe that stores them as datetimes.

Reproducing code example:

import numpy as np
import pandas as pd
df = pd.DataFrame([["2011-01-01", 14], ["2012-01-01", 25]], columns=["Date", "Amount"])
d1 = np.busday_count(df["Date"].iloc[0], df["Date"].iloc[1])
df["Date"] = pd.to_datetime(df["Date"])
d2 = np.busday_count(df["Date"].iloc[0].date(), df["Date"].iloc[1].date())
d3 = np.busday_count(df["Date"].iloc[0], df["Date"].iloc[1])

Error message:

In v1.14.0 and v1.14.3, the assignments to d1, d2 and d3 succeed and yield the same result. In v1.15.4, the assignment to d3 gives an error message:

in
----> 1 d3 = np.busday_count(df["Date"].iloc[0], df["Date"].iloc[1])

TypeError: Iterator operand 0 dtype could not be cast from dtype('<M8[us]') to dtype('<M8[D]') according to the rule 'safe'

Numpy/Python version information:

Versions on system where error occurs:

import sys, numpy, pandas; print(numpy.version, pandas.version, sys.version)
1.15.4 0.23.4 3.6.7 |Anaconda custom (64-bit)| (default, Dec 10 2018, 20:35:02) [MSC v.1915 64 bit (AMD64)]

Versions on system where error does not occur:

import sys, numpy, pandas; print(numpy.version, pandas.version, sys.version)
1.14.3 0.23.0 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
[GCC 7.2.0]

@jbrockmendel
Copy link
Contributor

This issue may be clarified by taking the pandas out of of it:

import numpy as np
from datetime import datetime

left = datetime(2011, 1, 1)
right = datetime(2012, 1, 1)

>>> np.busday_count(left, right)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 5, in busday_count
TypeError: Iterator operand 0 dtype could not be cast from dtype('<M8[us]') to dtype('<M8[D]') according to the rule 'safe'

(numpy 1.19.5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants