Skip to content

Remove support for DataFrames from df.from_records #57342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Removal of prior version deprecations/changes
- Removed ``use_nullable_dtypes`` from :func:`read_parquet` (:issue:`51853`)
- 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 support for ``errors="ignore"`` in :func:`to_datetime`, :func:`to_timedelta` and :func:`to_numeric` (:issue:`55734`)
- Removed the ``ArrayManager`` (:issue:`55043`)
- Removed the ``fastpath`` argument from the :class:`Series` constructor (:issue:`55466`)
Expand Down
20 changes: 3 additions & 17 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2112,11 +2112,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.
Expand Down Expand Up @@ -2184,21 +2181,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

Expand Down
11 changes: 0 additions & 11 deletions pandas/tests/copy_view/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,6 @@ def test_frame_from_numpy_array(copy):
assert np.shares_memory(get_array(df, 0), arr)


def test_dataframe_from_records_with_dataframe():
df = DataFrame({"a": [1, 2, 3]})
df_orig = df.copy()
with tm.assert_produces_warning(FutureWarning):
df2 = DataFrame.from_records(df)
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
tm.assert_frame_equal(df, df_orig)


def test_frame_from_dict_of_index():
idx = Index([1, 2, 3])
expected = idx.copy(deep=True)
Expand Down
48 changes: 4 additions & 44 deletions pandas/tests/frame/constructors/test_from_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down