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

API, DEPR: Raise and Deprecate Reshape for Pandas Objects #13012

Closed

Conversation

gfyoung
Copy link
Member

@gfyoung gfyoung commented Apr 27, 2016

reshape largely exists for compat reasons with numpy but in the interests of moving away from that, this PR will cause objects like Series, Index, and Categorical to deprecate OR raise when such a method is called.

@@ -358,6 +358,19 @@ def itemsize(self):

def reshape(self, new_shape, **kwargs):
""" compat with .reshape """
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 make this a more propert doc-string (e.g. Parameters)

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.

@jreback jreback added the Compat pandas objects compatability with Numpy or Python functions label Apr 27, 2016
@jreback jreback added this to the 0.18.1 milestone Apr 27, 2016
@jreback
Copy link
Contributor

jreback commented Apr 27, 2016

need these types of checks for Index as well. might as well combine the checks for Categorical,Index,Series (e.g. make a method to do it in core/base)

@shoyer
Copy link
Member

shoyer commented Apr 27, 2016

This is definitely a good idea 👍

@kawochen
Copy link
Contributor

had a glance. don't think you are handling (-2, -1)?

@gfyoung
Copy link
Member Author

gfyoung commented Apr 27, 2016

@jreback: Sure thing

@kawochen : Yikes, good catch. Will fix.

@gfyoung
Copy link
Member Author

gfyoung commented Apr 28, 2016

@jreback : Actually, on second thought:

  1. Index has no reshape method --> but is this PR the right place to add such a method (is it really necessary even?)

  2. Series may not need such checks IMO because it passes new_shape to its _values attribute for reshaping. AFAICT that _values attribute is either ndarray (or sub-class) OR Categorical. Seems like duplicate work to have it validate in Series and then have to validate again in _values. Thoughts?

@gfyoung gfyoung force-pushed the categorical-reshape-validate branch 2 times, most recently from 208eaa8 to 2c7160e Compare April 28, 2016 08:15
@jreback
Copy link
Contributor

jreback commented Apr 28, 2016

Index should have one (that should just raise), as will Series in the next release. So this is just a bandaid. Categorical should raise as well, its an unsupported method.

@gfyoung
Copy link
Member Author

gfyoung commented Apr 28, 2016

  1. The raising should be for a separate PR, right?
  2. My point was that I saw no reason to perform validation twice given what _values can be. In light of your first point, I do agree that this is only a band-aid at best.

@jreback jreback removed this from the 0.18.1 milestone Apr 28, 2016
@jreback
Copy link
Contributor

jreback commented Apr 28, 2016

see my point above

@gfyoung
Copy link
Member Author

gfyoung commented Apr 28, 2016

Ah, okay, I see. Let them all raise then. Should be relatively straightforward.

@gfyoung gfyoung changed the title API: Prevent invalid arguments to Categorical.reshape API: Raise on Reshape for Pandas Objects Apr 28, 2016
@jreback
Copy link
Contributor

jreback commented Apr 28, 2016

@gfyoung but Series you can't change now as its technically an API change. however, would be ok with deprecating it now.

@gfyoung
Copy link
Member Author

gfyoung commented Apr 28, 2016

Shouldn't we do the same with Categorical then?

@gfyoung gfyoung changed the title API: Raise on Reshape for Pandas Objects API, DEPR: Raise and Deprecate Reshape for Pandas Objects Apr 28, 2016
@jreback
Copy link
Contributor

jreback commented Apr 28, 2016

yes, didn't realize that .reshape was already defined.

@gfyoung
Copy link
Member Author

gfyoung commented Apr 28, 2016

Alrighty, so I'll deprecate for both Series and Categorical and add a method for Index that raises.

@gfyoung gfyoung force-pushed the categorical-reshape-validate branch from 2c7160e to 5498dc9 Compare April 28, 2016 20:22
@codecov-io
Copy link

codecov-io commented Apr 28, 2016

Current coverage is 84.38%

Merging #13012 into master will increase coverage by <.01%

@@             master     #13012   diff @@
==========================================
  Files           142        142          
  Lines         51225      51235    +10   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits          43225      43234     +9   
- Misses         8000       8001     +1   
  Partials          0          0          

Powered by Codecov. Last updated by 20de266...3ad161d

@gfyoung
Copy link
Member Author

gfyoung commented Apr 29, 2016

@jreback : I've added deprecation and raising behaviour for Series, Index, and Categorical. Travis gives a green light, so this is ready to merge if there is nothing else.

@jreback jreback added this to the 0.19.0 milestone Apr 29, 2016
@jreback
Copy link
Contributor

jreback commented Apr 29, 2016

let's do this in 0.19.0

@gfyoung gfyoung force-pushed the categorical-reshape-validate branch 4 times, most recently from e77529e to e902d79 Compare April 30, 2016 21:41
by `new_shape` must be the same as that of the original
shape of the `Categorical`. One shape dimension can be
negative. In this case, the value is inferred from the
length of the array and the remaining dimensions.
Copy link
Member

Choose a reason for hiding this comment

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

Is it needed to add this documentation on new_shape if we are deprecating its usage?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, would make these really simple.

Copy link
Member Author

@gfyoung gfyoung Jul 11, 2016

Choose a reason for hiding this comment

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

As this is really a numpy thing, I'm just going to point the documentation on new_shape to np.reshape (we're deprecating this function after all, so I don't feel too bad for tying it to numpy).

@jorisvandenbossche
Copy link
Member

@jreback What is the reason of adding a Index.reshape that raises? That seems unnecessary to me (and only adds to the list of Index methods).

For people using Series.reshape (not sure why they would do it, but probably it is used in the wild), is Series.values.reshape the alternative they should use instead?
If so, maybe add to the depr message?

@jreback
Copy link
Contributor

jreback commented Jul 11, 2016

@jorisvandenbossche we dont technically need this on Index as the idea is to raise on Series/Categorical in the future. (it should have been added originally to Index).

@gfyoung gfyoung force-pushed the categorical-reshape-validate branch from 24b6067 to 3860bc2 Compare July 11, 2016 13:22
@gfyoung
Copy link
Member Author

gfyoung commented Jul 12, 2016

@jreback , @jorisvandenbossche : Made requested doc changes (no need to run Travis on those, hence the [ci skip]), so ready to merge if there are no other concerns.

@jorisvandenbossche
Copy link
Member

@gfyoung See my comment from above: #13012 (comment)

@gfyoung
Copy link
Member Author

gfyoung commented Jul 12, 2016

@jorisvandenbossche: Ah whoops, good point. Will do.

@gfyoung gfyoung force-pushed the categorical-reshape-validate branch from 3860bc2 to 118d75f Compare July 13, 2016 02:06
# and Series and will be removed in a subsequent version,
# hence the two checks done before calling 'reshape'
if isinstance(value, ABCSeries):
value = value._values
Copy link
Contributor

Choose a reason for hiding this comment

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

what is this for?

Copy link
Contributor

Choose a reason for hiding this comment

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

oh I see.

ok, need an internal function to do this then (rather than having this duplicated code)

put in pandas.types.concat maybe compat_reshape

Copy link
Member Author

@gfyoung gfyoung Jul 13, 2016

Choose a reason for hiding this comment

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

  1. It was throwing warnings during testing.

  2. The current code for Series.reshape will simply call self._values.reshape(...), so I am preempting that so we don't have to call the reshape method for Series.

Copy link
Contributor

Choose a reason for hiding this comment

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

and that's fine, except for have code duplication, so just put it in an internal function as I said above

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't get that second note until after I posted mine. 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Though why pandas.types.concat? We aren't really concatenating anything here. My first inclination was to try to put in pandas.compat, but I don't see a good home for it there.

Copy link
Contributor

Choose a reason for hiding this comment

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

u can also put in internals

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good. Done.

@gfyoung gfyoung force-pushed the categorical-reshape-validate branch from 118d75f to e849f9c Compare July 13, 2016 08:09
@@ -944,6 +944,16 @@ def rename(self, name, inplace=False):
"""
return self.set_names([name], inplace=inplace)

def reshape(self, *args, **kwargs):
"""
Reshape an Index.
Copy link
Member

Choose a reason for hiding this comment

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

Can you put "Not implemented" on the first line? As otherwise in the methods overview in the Index docstring, you will see the reshape method with this explanation, and from that it would seem it has a working reshape method

Copy link
Member Author

Choose a reason for hiding this comment

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

Will do as soon as Travis gives a green light.

@jreback
Copy link
Contributor

jreback commented Jul 13, 2016

lgtm (minor doc comments). will need a rebase after #13147

@gfyoung gfyoung force-pushed the categorical-reshape-validate branch 3 times, most recently from 9c4e7a9 to 229060b Compare July 13, 2016 14:42
@gfyoung gfyoung force-pushed the categorical-reshape-validate branch from 229060b to 3ad161d Compare July 13, 2016 14:43
@gfyoung
Copy link
Member Author

gfyoung commented Jul 14, 2016

@jreback : Made the requested doc changes as well as rebased, and Travis is still passing. Ready to merge if there are no other concerns.

@jreback jreback closed this in 084ceae Jul 14, 2016
@jreback
Copy link
Contributor

jreback commented Jul 14, 2016

thanks @gfyoung

@gfyoung gfyoung deleted the categorical-reshape-validate branch July 14, 2016 14:52
gfyoung added a commit to forking-repos/pandas that referenced this pull request Dec 27, 2017
Remove the method for Series, Categorical,
and Index. Deprecated or errored in v0.19.0

xref pandas-devgh-13012
jreback pushed a commit that referenced this pull request Dec 27, 2017
Remove the method for Series, Categorical,
and Index. Deprecated or errored in v0.19.0

xref gh-13012
hexgnu pushed a commit to hexgnu/pandas that referenced this pull request Dec 28, 2017
Remove the method for Series, Categorical,
and Index. Deprecated or errored in v0.19.0

xref pandas-devgh-13012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants