From c2139ae5a0ce92ad1fecae531dd03356ed1a4c0e Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sat, 10 Feb 2024 23:42:16 +0100 Subject: [PATCH 1/3] Remove support for DataFrames from df.from_records --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/frame.py | 15 +----- .../frame/constructors/test_from_records.py | 48 ++----------------- 3 files changed, 7 insertions(+), 57 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 8b8f5bf3d028c..88335760bb3cc 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -122,6 +122,7 @@ Removal of prior version deprecations/changes - Removed ``read_gbq`` and ``DataFrame.to_gbq``. Use ``pandas_gbq.read_gbq`` and ``pandas_gbq.to_gbq`` instead https://pandas-gbq.readthedocs.io/en/latest/api.html (:issue:`55525`) - Removed deprecated argument ``obj`` in :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` (:issue:`53545`) - Removed deprecated behavior of :meth:`Series.agg` using :meth:`Series.apply` (:issue:`53325`) +- Removed support for :class:`DataFrame` in :meth:`DataFrame.from_records`(:issue:`51697`) - Removed the ``ArrayManager`` (:issue:`55043`) - Removed the ``fastpath`` argument from the :class:`Series` constructor (:issue:`55466`) - Removed the ``is_boolean``, ``is_integer``, ``is_floating``, ``holds_integer``, ``is_numeric``, ``is_categorical``, ``is_object``, and ``is_interval`` attributes of :class:`Index` (:issue:`50042`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 591c02933c4bc..56cbf97165f5e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2185,21 +2185,10 @@ def from_records( 3 0 d """ if isinstance(data, DataFrame): - warnings.warn( - "Passing a DataFrame to DataFrame.from_records is deprecated. Use " + raise TypeError( + "Passing a DataFrame to DataFrame.from_records is not supported. Use " "set_index and/or drop to modify the DataFrame instead.", - FutureWarning, - stacklevel=find_stack_level(), ) - if columns is not None: - if is_scalar(columns): - columns = [columns] - data = data[columns] - if index is not None: - data = data.set_index(index) - if exclude is not None: - data = data.drop(columns=exclude) - return data.copy(deep=False) result_index = None diff --git a/pandas/tests/frame/constructors/test_from_records.py b/pandas/tests/frame/constructors/test_from_records.py index 3622571f1365d..66fc234e79b4d 100644 --- a/pandas/tests/frame/constructors/test_from_records.py +++ b/pandas/tests/frame/constructors/test_from_records.py @@ -17,19 +17,16 @@ Interval, RangeIndex, Series, - date_range, ) import pandas._testing as tm class TestFromRecords: def test_from_records_dt64tz_frame(self): - # GH#51162 don't lose tz when calling from_records with DataFrame input - dti = date_range("2016-01-01", periods=10, tz="US/Pacific") - df = DataFrame({i: dti for i in range(4)}) - with tm.assert_produces_warning(FutureWarning): - res = DataFrame.from_records(df) - tm.assert_frame_equal(res, df) + # GH#51697 + df = DataFrame({"a": [1, 2, 3]}) + with pytest.raises(TypeError, match="not supported"): + DataFrame.from_records(df) def test_from_records_with_datetimes(self): # this may fail on certain platforms because of a numpy issue @@ -195,43 +192,6 @@ def test_from_records_dictlike(self): for r in results: tm.assert_frame_equal(r, df) - def test_from_records_with_index_data(self): - df = DataFrame( - np.random.default_rng(2).standard_normal((10, 3)), columns=["A", "B", "C"] - ) - - data = np.random.default_rng(2).standard_normal(10) - with tm.assert_produces_warning(FutureWarning): - df1 = DataFrame.from_records(df, index=data) - tm.assert_index_equal(df1.index, Index(data)) - - def test_from_records_bad_index_column(self): - df = DataFrame( - np.random.default_rng(2).standard_normal((10, 3)), columns=["A", "B", "C"] - ) - - # should pass - with tm.assert_produces_warning(FutureWarning): - df1 = DataFrame.from_records(df, index=["C"]) - tm.assert_index_equal(df1.index, Index(df.C)) - - with tm.assert_produces_warning(FutureWarning): - df1 = DataFrame.from_records(df, index="C") - tm.assert_index_equal(df1.index, Index(df.C)) - - # should fail - msg = "|".join( - [ - r"'None of \[2\] are in the columns'", - ] - ) - with pytest.raises(KeyError, match=msg): - with tm.assert_produces_warning(FutureWarning): - DataFrame.from_records(df, index=[2]) - with pytest.raises(KeyError, match=msg): - with tm.assert_produces_warning(FutureWarning): - DataFrame.from_records(df, index=2) - def test_from_records_non_tuple(self): class Record: def __init__(self, *args) -> None: From c4fc474a5765bde33111539fc78b924ad7c2cc54 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sat, 10 Feb 2024 23:43:37 +0100 Subject: [PATCH 2/3] Fix docs --- pandas/core/frame.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 56cbf97165f5e..83205e6cb3ee7 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2113,11 +2113,8 @@ def from_records( Parameters ---------- - data : structured ndarray, sequence of tuples or dicts, or DataFrame + data : structured ndarray, sequence of tuples or dicts Structured input data. - - .. deprecated:: 2.1.0 - Passing a DataFrame is deprecated. index : str, list of fields, array-like Field of array to use as the index, alternately a specific set of input labels to use. From 3c37f83dcf18ea932c22012dd17eaad357244d13 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sun, 11 Feb 2024 00:04:58 +0100 Subject: [PATCH 3/3] Update --- pandas/tests/copy_view/test_constructors.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/pandas/tests/copy_view/test_constructors.py b/pandas/tests/copy_view/test_constructors.py index 77c07d11e3381..f337caa971032 100644 --- a/pandas/tests/copy_view/test_constructors.py +++ b/pandas/tests/copy_view/test_constructors.py @@ -311,21 +311,6 @@ def test_frame_from_numpy_array(using_copy_on_write, copy): assert np.shares_memory(get_array(df, 0), arr) -def test_dataframe_from_records_with_dataframe(using_copy_on_write): - df = DataFrame({"a": [1, 2, 3]}) - df_orig = df.copy() - with tm.assert_produces_warning(FutureWarning): - df2 = DataFrame.from_records(df) - if using_copy_on_write: - assert not df._mgr._has_no_reference(0) - assert np.shares_memory(get_array(df, "a"), get_array(df2, "a")) - df2.iloc[0, 0] = 100 - if using_copy_on_write: - tm.assert_frame_equal(df, df_orig) - else: - tm.assert_frame_equal(df, df2) - - def test_frame_from_dict_of_index(using_copy_on_write): idx = Index([1, 2, 3]) expected = idx.copy(deep=True)