diff --git a/python/cudf/cudf/core/_base_index.py b/python/cudf/cudf/core/_base_index.py index d56345a133d..3616ec1b542 100644 --- a/python/cudf/cudf/core/_base_index.py +++ b/python/cudf/cudf/core/_base_index.py @@ -1836,7 +1836,7 @@ def __array_function__(self, func, types, args, kwargs): return NotImplemented @classmethod - def from_pandas(cls, index, nan_as_null=None): + def from_pandas(cls, index, nan_as_null=no_default): """ Convert from a Pandas Index. @@ -1866,6 +1866,11 @@ def from_pandas(cls, index, nan_as_null=None): >>> cudf.Index.from_pandas(pdi, nan_as_null=False) Float64Index([10.0, 20.0, 30.0, nan], dtype='float64') """ + if nan_as_null is no_default: + nan_as_null = ( + False if cudf.get_option("mode.pandas_compatible") else None + ) + if not isinstance(index, pd.Index): raise TypeError("not a pandas.Index") diff --git a/python/cudf/cudf/core/dataframe.py b/python/cudf/cudf/core/dataframe.py index 89e1e58516a..46c7557148e 100644 --- a/python/cudf/cudf/core/dataframe.py +++ b/python/cudf/cudf/core/dataframe.py @@ -5184,7 +5184,7 @@ def to_pandas(self, nullable=False, **kwargs): @classmethod @_cudf_nvtx_annotate - def from_pandas(cls, dataframe, nan_as_null=None): + def from_pandas(cls, dataframe, nan_as_null=no_default): """ Convert from a Pandas DataFrame. @@ -5213,6 +5213,11 @@ def from_pandas(cls, dataframe, nan_as_null=None): 1 1 2 2 3 4 """ + if nan_as_null is no_default: + nan_as_null = ( + False if cudf.get_option("mode.pandas_compatible") else None + ) + if isinstance(dataframe, pd.DataFrame): if not dataframe.columns.is_unique: @@ -7770,7 +7775,7 @@ def func(left, right, output): @_cudf_nvtx_annotate -def from_pandas(obj, nan_as_null=None): +def from_pandas(obj, nan_as_null=no_default): """ Convert certain Pandas objects into the cudf equivalent. @@ -7868,6 +7873,11 @@ def from_pandas(obj, nan_as_null=None): >>> type(pmidx) """ + if nan_as_null is no_default: + nan_as_null = ( + False if cudf.get_option("mode.pandas_compatible") else None + ) + if isinstance(obj, pd.DataFrame): return DataFrame.from_pandas(obj, nan_as_null=nan_as_null) elif isinstance(obj, pd.Series): diff --git a/python/cudf/cudf/core/multiindex.py b/python/cudf/cudf/core/multiindex.py index 8c05c7a328e..c904d1b021b 100644 --- a/python/cudf/cudf/core/multiindex.py +++ b/python/cudf/cudf/core/multiindex.py @@ -1604,7 +1604,7 @@ def to_pandas(self, nullable=False, **kwargs): @classmethod @_cudf_nvtx_annotate - def from_pandas(cls, multiindex, nan_as_null=None): + def from_pandas(cls, multiindex, nan_as_null=no_default): """ Convert from a Pandas MultiIndex @@ -1625,6 +1625,10 @@ def from_pandas(cls, multiindex, nan_as_null=None): """ if not isinstance(multiindex, pd.MultiIndex): raise TypeError("not a pandas.MultiIndex") + if nan_as_null is no_default: + nan_as_null = ( + False if cudf.get_option("mode.pandas_compatible") else None + ) # if `multiindex` has two or more levels that # have the same name, then `multiindex.to_frame()` diff --git a/python/cudf/cudf/core/series.py b/python/cudf/cudf/core/series.py index cadd53fa516..66e8d00a934 100644 --- a/python/cudf/cudf/core/series.py +++ b/python/cudf/cudf/core/series.py @@ -688,7 +688,7 @@ def __contains__(self, item): @classmethod @_cudf_nvtx_annotate - def from_pandas(cls, s, nan_as_null=None): + def from_pandas(cls, s, nan_as_null=no_default): """ Convert from a Pandas Series. @@ -726,6 +726,10 @@ def from_pandas(cls, s, nan_as_null=None): 3 NaN dtype: float64 """ + if nan_as_null is no_default: + nan_as_null = ( + False if cudf.get_option("mode.pandas_compatible") else None + ) with warnings.catch_warnings(): warnings.simplefilter("ignore") result = cls(s, nan_as_null=nan_as_null) diff --git a/python/cudf/cudf/tests/test_series.py b/python/cudf/cudf/tests/test_series.py index b0cd9937835..e606c36e855 100644 --- a/python/cudf/cudf/tests/test_series.py +++ b/python/cudf/cudf/tests/test_series.py @@ -2575,3 +2575,10 @@ def test_series_categorical_missing_value_count(): actual = gs.value_counts() assert_eq(expected, actual, check_dtype=False) + + +def test_series_error_nan_mixed_types(): + ps = pd.Series([np.nan, "ab", "cd"]) + with cudf.option_context("mode.pandas_compatible", True): + with pytest.raises(pa.ArrowInvalid): + cudf.from_pandas(ps)