From 01b5d26c559dfddc8a4fd30ddf89d579a336c594 Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Mon, 9 Sep 2024 01:11:28 -0400 Subject: [PATCH] silence infer_objects warning for object backed string type --- pandas/core/frame.py | 7 ++++++- pandas/core/indexes/base.py | 8 +++++++- pandas/core/series.py | 8 +++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 1403fc2ceaaf8..7a61533e46007 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -923,7 +923,12 @@ def __init__( NDFrame.__init__(self, mgr) - if original_dtype is None and is_pandas_object and data_dtype == np.object_: + if ( + original_dtype is None + and is_pandas_object + and data_dtype == np.object_ + and self.dtypes.iloc[0] != "str" + ): if self.dtypes.iloc[0] != data_dtype: warnings.warn( "Dtype inference on a pandas object " diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 825316585c03c..7be899877e0a8 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -575,7 +575,13 @@ def __new__( arr = klass._ensure_array(arr, arr.dtype, copy=False) result = klass._simple_new(arr, name, refs=refs) - if dtype is None and is_pandas_object and data_dtype == np.object_: + # TODO: also verify this check + if ( + dtype is None + and is_pandas_object + and data_dtype == np.object_ + and result.dtype != "str" + ): if result.dtype != data_dtype: warnings.warn( "Dtype inference on a pandas object " diff --git a/pandas/core/series.py b/pandas/core/series.py index 6fd019656d207..042c36558f4a1 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -593,7 +593,13 @@ def __init__( self.name = name self._set_axis(0, index) - if original_dtype is None and is_pandas_object and data_dtype == np.object_: + # TODO: verify we're silencing the warning correctly here + if ( + original_dtype is None + and is_pandas_object + and data_dtype == np.object_ + and self.dtype != "str" + ): if self.dtype != data_dtype: warnings.warn( "Dtype inference on a pandas object "