Skip to content

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

Merged
merged 3 commits into from
Oct 19, 2017

Conversation

jschendel
Copy link
Member

Had to modify a few existing tests that use range, as this caused them to fail due to dtypes mismatch; mostly just involved switching to np.arange.

elif isinstance(data, range):
# GH 16804
start, stop, step = _get_range_parameters(data)
if is_extension_type(dtype):
Copy link
Contributor

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

Copy link
Member Author

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.

@gfyoung gfyoung added Bug Compat pandas objects compatability with Numpy or Python functions Dtype Conversions Unexpected or buggy dtype conversions labels Oct 12, 2017
elif isinstance(data, range):
# GH 16804
start, stop, step = _get_range_parameters(data)
if is_extension_type(dtype):
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -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"""
Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -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()
Copy link
Contributor

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?

Copy link
Member Author

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.

@codecov
Copy link

codecov bot commented Oct 18, 2017

Codecov Report

❗ No coverage uploaded for pull request base (master@5687f9e). Click here to learn what that means.
The diff coverage is 42.85%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master   #17840   +/-   ##
=========================================
  Coverage          ?   91.22%           
=========================================
  Files             ?      163           
  Lines             ?    50112           
  Branches          ?        0           
=========================================
  Hits              ?    45713           
  Misses            ?     4399           
  Partials          ?        0
Flag Coverage Δ
#multiple 89.03% <42.85%> (?)
#single 40.31% <42.85%> (?)
Impacted Files Coverage Δ
pandas/core/series.py 95% <100%> (ø)
pandas/core/indexes/range.py 95.66% <100%> (ø)
pandas/compat/__init__.py 58.1% <14.28%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5687f9e...dfa4573. Read the comment docs.

Copy link
Contributor

@jreback jreback left a 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.

@@ -146,6 +150,24 @@ def bytes_to_str(b, encoding='ascii'):
def signature(f):
return inspect.getargspec(f)

def _get_range_parameters(data):
Copy link
Contributor

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 _)

@jreback jreback added this to the 0.21.0 milestone Oct 18, 2017
@jschendel
Copy link
Member Author

ping @jreback : green

@jreback jreback merged commit a2e5400 into pandas-dev:master Oct 19, 2017
@jreback
Copy link
Contributor

jreback commented Oct 19, 2017

thanks @jschendel !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Compat pandas objects compatability with Numpy or Python functions Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Wrong dtype using range in DataFrame constructor on Windows
4 participants