-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fix range dtype in Series/DataFrame constructor on Windows #17840
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
Conversation
pandas/core/series.py
Outdated
elif isinstance(data, range): | ||
# GH 16804 | ||
start, stop, step = _get_range_parameters(data) | ||
if is_extension_type(dtype): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What case does this handle? Doesn't seem to be a matching test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for dtypes that are specific to pandas that numpy doesn't understand, e.g. pd.Series(range(5), dtype='category')
. Will add a test for this when I fix the failing tests.
pandas/core/series.py
Outdated
elif isinstance(data, range): | ||
# GH 16804 | ||
start, stop, step = _get_range_parameters(data) | ||
if is_extension_type(dtype): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you construct the range immediately after _get_range_parameters
. I think this might just work then (you still prob need the _try_cast
); I don't think its a big deal to always construct the range in int64, then cast if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
pandas/core/common.py
Outdated
@@ -539,6 +539,30 @@ class Sentinel(object): | |||
return Sentinel() | |||
|
|||
|
|||
def _get_range_parameters(data): | |||
"""Gets the start, stop, and step parameters from a range object""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe put this in pandas/compat/__init__
instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
pandas/tests/test_resample.py
Outdated
@@ -3030,7 +3030,7 @@ def test_nearest(self): | |||
# GH 17496 | |||
# Resample nearest | |||
index = pd.date_range('1/1/2000', periods=3, freq='T') | |||
result = pd.Series(range(3), index=index).resample('20s').nearest() | |||
result = pd.Series(np.arange(3), index=index).resample('20s').nearest() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so I would actually leave some tests with pure range in the constructor, e.g. is there a reason to change this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The expected is pd.Series(np.array([0, ...]), index=...)
, and on Windows the np.array
defaults to int32
dtype, so the test fails since result is now int64
dtype.
Either need range
-> np.arange
or np.array
-> list
(or specify the dtype in np.array
) for the test to pass. Changed it to keep range
here.
77e986c
to
58adb2e
Compare
Codecov Report
@@ Coverage Diff @@
## master #17840 +/- ##
=========================================
Coverage ? 91.22%
=========================================
Files ? 163
Lines ? 50112
Branches ? 0
=========================================
Hits ? 45713
Misses ? 4399
Partials ? 0
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trivial change, ping on green.
pandas/compat/__init__.py
Outdated
@@ -146,6 +150,24 @@ def bytes_to_str(b, encoding='ascii'): | |||
def signature(f): | |||
return inspect.getargspec(f) | |||
|
|||
def _get_range_parameters(data): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can de-privatize these (e.g. remove the leading _)
58adb2e
to
dfa4573
Compare
ping @jreback : green |
thanks @jschendel ! |
range
in DataFrame constructor on Windows #16804git diff upstream/master -u -- "*.py" | flake8 --diff
Had to modify a few existing tests that use
range
, as this caused them to fail due to dtypes mismatch; mostly just involved switching tonp.arange
.