ENH: Add downcast as method to DataFrame and Series
State: closed

- [ ] xref #40988 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.

@jbrockmendel adding downcast as first step towards getting rid of the keyword for fillna and related

Labels:
name: Stale

Comments:
Do we have an idea how often anything other than "infer" is used? im not wild about a try/except for the non-infer cases, and without it users might as well just user .astype
Fair point. I don't think very often, but not sure. We could just try it with infer and see if there are any reports that people are using it?
> We could just try it with infer and see if there are any reports that people are using it?

you mean just enable "infer" in the new method and see if anyone asks for the other options?  that seems fine
Yes exactly
Removed the argument
couple comments, otherwise LGTM.  id like to make sure we have buy-in from the rest of the team on doing this and deprecating the keyword
Sounds good to me. Should we ping on the issue?
+1 on the deprecation itself
to_numeric doesn't work well on DataFrames, but also it changes the size of your dtype if possible, downcast here only coerces from float to int if possible
sure but maybe that's a better option

eg enhancing to_numeric to accept a frame and also a less strict option
Hm I think calling to_numeric on an already numeric object is a bit counterintuitive? Should we rather mirror the functionality from to_numeric here and centralise down casting logic here?
that's another option 

yeah just shouldnt be different very similar options - 
@jbrockmendel thoughts?
> downcast here only coerces from float to int if possible

This might be confusing for a method called "downcast"? When I saw this issue, I assumed it would be about a method that would actually downcast things like int64 -> int32 etc (that's also what the `downcast` keyword in to_numeric does). 
The current docstring in the PR also isn't very clear about that this is the only thing it does.

Adding the actual downcasting logic that to_numeric has might make sense, then.

But, personally, I am not sure this is worth a completely new DataFrame/Series method. 

And if we want something like this, I am wondering if it would be more interesting to have something more general like a "infer dtypes" (or "optimize" dtypes) method (although it would then be a but unfortunate that we already have both an `infer_objects` and `convert_dtypes` that both do something a bit different). "Inferring dtypes" could have options to enable several kinds of dtype optimizations, like float->int, smaller bitwidths for float/int, inferring object, ... (and later maybe things like converting to categorical for certain characteristics such as low cardinality strings)


Yeah I agree that we should add the down casting logic from to_numeric.

Right now all of them are doing something different. Goal is to centralise this here and also get rid of the inconsistent behavior in fillna and friends.
How to centralize/standardize downcasting in a minimal set of methods is hard, as is naming.

The "real" motivation is getting the downcasting out of fillna etc.  I think the lazy thing to do is deprecate that first then see if anyone complains, then add this if necessary.  That avoids the difficult part (choosing a name).
This pull request is stale because it has been open for thirty days with no activity. Please [update](https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#updating-your-pull-request) and respond to this comment if you're still interested in working on this.
> The "real" motivation is getting the downcasting out of fillna etc. I think the lazy thing to do is deprecate that first then see if anyone complains, then add this if necessary. That avoids the difficult part (choosing a name).

i've stumbled upon the annoying downcast-inside-fillna/where again.  Gonna go ahead and deprecate at least some of that.
Sounds good, let's close this for now till we get actual complaints. Should I take care of downcast keywords?