@@ -3671,16 +3671,66 @@ def convert_objects(self, convert_dates=True, convert_numeric=False,
3671
3671
converted : same as input object
3672
3672
"""
3673
3673
from warnings import warn
3674
- warn ("convert_objects is deprecated. Use the data-type specific "
3675
- "converters pd.to_datetime, pd.to_timedelta and pd.to_numeric." ,
3676
- FutureWarning , stacklevel = 2 )
3674
+ msg = ("convert_objects is deprecated. To re-infer data dtypes for "
3675
+ "object columns, use {klass}.infer_objects()\n For all "
3676
+ "other conversions use the data-type specific converters "
3677
+ "pd.to_datetime, pd.to_timedelta and pd.to_numeric."
3678
+ ).format (klass = self .__class__ .__name__ )
3679
+ warn (msg , FutureWarning , stacklevel = 2 )
3677
3680
3678
3681
return self ._constructor (
3679
3682
self ._data .convert (convert_dates = convert_dates ,
3680
3683
convert_numeric = convert_numeric ,
3681
3684
convert_timedeltas = convert_timedeltas ,
3682
3685
copy = copy )).__finalize__ (self )
3683
3686
3687
+ def infer_objects (self ):
3688
+ """
3689
+ Attempt to infer better dtypes for object columns.
3690
+
3691
+ Attempts soft conversion of object-dtyped
3692
+ columns, leaving non-object and unconvertible
3693
+ columns unchanged. The inference rules are the
3694
+ same as during normal Series/DataFrame construction.
3695
+
3696
+ .. versionadded:: 0.20.0
3697
+
3698
+ See Also
3699
+ --------
3700
+ pandas.to_datetime : Convert argument to datetime.
3701
+ pandas.to_timedelta : Convert argument to timedelta.
3702
+ pandas.to_numeric : Convert argument to numeric typeR
3703
+
3704
+ Returns
3705
+ -------
3706
+ converted : same type as input object
3707
+
3708
+ Examples
3709
+ --------
3710
+ >>> df = pd.DataFrame({"A": ["a", 1, 2, 3]})
3711
+ >>> df = df.iloc[1:]
3712
+ >>> df
3713
+ A
3714
+ 1 1
3715
+ 2 2
3716
+ 3 3
3717
+
3718
+ >>> df.dtypes
3719
+ A object
3720
+ dtype: object
3721
+
3722
+ >>> df.infer_objects().dtypes
3723
+ A int64
3724
+ dtype: object
3725
+ """
3726
+ # numeric=False necessary to only soft convert;
3727
+ # python objects will still be converted to
3728
+ # native numpy numeric types
3729
+ return self ._constructor (
3730
+ self ._data .convert (datetime = True , numeric = False ,
3731
+ timedelta = True , coerce = False ,
3732
+ copy = True )).__finalize__ (self )
3733
+
3684
3734
# ----------------------------------------------------------------------
3685
3735
# Filling NA's
3686
3736
0 commit comments