Skip to content

Commit fda31c4

Browse files
phoflpmhatre1
authored andcommitted
Remove support for DataFrames from df.from_records (pandas-dev#57342)
* Remove support for DataFrames from df.from_records * Fix docs * Update
1 parent 6e96215 commit fda31c4

File tree

4 files changed

+8
-72
lines changed

4 files changed

+8
-72
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Removal of prior version deprecations/changes
130130
- Removed ``use_nullable_dtypes`` from :func:`read_parquet` (:issue:`51853`)
131131
- Removed deprecated argument ``obj`` in :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` (:issue:`53545`)
132132
- Removed deprecated behavior of :meth:`Series.agg` using :meth:`Series.apply` (:issue:`53325`)
133+
- Removed support for :class:`DataFrame` in :meth:`DataFrame.from_records`(:issue:`51697`)
133134
- Removed support for ``errors="ignore"`` in :func:`to_datetime`, :func:`to_timedelta` and :func:`to_numeric` (:issue:`55734`)
134135
- Removed the ``ArrayManager`` (:issue:`55043`)
135136
- Removed the ``fastpath`` argument from the :class:`Series` constructor (:issue:`55466`)

pandas/core/frame.py

+3-17
Original file line numberDiff line numberDiff line change
@@ -2112,11 +2112,8 @@ def from_records(
21122112
21132113
Parameters
21142114
----------
2115-
data : structured ndarray, sequence of tuples or dicts, or DataFrame
2115+
data : structured ndarray, sequence of tuples or dicts
21162116
Structured input data.
2117-
2118-
.. deprecated:: 2.1.0
2119-
Passing a DataFrame is deprecated.
21202117
index : str, list of fields, array-like
21212118
Field of array to use as the index, alternately a specific set of
21222119
input labels to use.
@@ -2184,21 +2181,10 @@ def from_records(
21842181
3 0 d
21852182
"""
21862183
if isinstance(data, DataFrame):
2187-
warnings.warn(
2188-
"Passing a DataFrame to DataFrame.from_records is deprecated. Use "
2184+
raise TypeError(
2185+
"Passing a DataFrame to DataFrame.from_records is not supported. Use "
21892186
"set_index and/or drop to modify the DataFrame instead.",
2190-
FutureWarning,
2191-
stacklevel=find_stack_level(),
21922187
)
2193-
if columns is not None:
2194-
if is_scalar(columns):
2195-
columns = [columns]
2196-
data = data[columns]
2197-
if index is not None:
2198-
data = data.set_index(index)
2199-
if exclude is not None:
2200-
data = data.drop(columns=exclude)
2201-
return data.copy(deep=False)
22022188

22032189
result_index = None
22042190

pandas/tests/copy_view/test_constructors.py

-11
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,6 @@ def test_frame_from_numpy_array(copy):
264264
assert np.shares_memory(get_array(df, 0), arr)
265265

266266

267-
def test_dataframe_from_records_with_dataframe():
268-
df = DataFrame({"a": [1, 2, 3]})
269-
df_orig = df.copy()
270-
with tm.assert_produces_warning(FutureWarning):
271-
df2 = DataFrame.from_records(df)
272-
assert not df._mgr._has_no_reference(0)
273-
assert np.shares_memory(get_array(df, "a"), get_array(df2, "a"))
274-
df2.iloc[0, 0] = 100
275-
tm.assert_frame_equal(df, df_orig)
276-
277-
278267
def test_frame_from_dict_of_index():
279268
idx = Index([1, 2, 3])
280269
expected = idx.copy(deep=True)

pandas/tests/frame/constructors/test_from_records.py

+4-44
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@
1717
Interval,
1818
RangeIndex,
1919
Series,
20-
date_range,
2120
)
2221
import pandas._testing as tm
2322

2423

2524
class TestFromRecords:
2625
def test_from_records_dt64tz_frame(self):
27-
# GH#51162 don't lose tz when calling from_records with DataFrame input
28-
dti = date_range("2016-01-01", periods=10, tz="US/Pacific")
29-
df = DataFrame({i: dti for i in range(4)})
30-
with tm.assert_produces_warning(FutureWarning):
31-
res = DataFrame.from_records(df)
32-
tm.assert_frame_equal(res, df)
26+
# GH#51697
27+
df = DataFrame({"a": [1, 2, 3]})
28+
with pytest.raises(TypeError, match="not supported"):
29+
DataFrame.from_records(df)
3330

3431
def test_from_records_with_datetimes(self):
3532
# this may fail on certain platforms because of a numpy issue
@@ -195,43 +192,6 @@ def test_from_records_dictlike(self):
195192
for r in results:
196193
tm.assert_frame_equal(r, df)
197194

198-
def test_from_records_with_index_data(self):
199-
df = DataFrame(
200-
np.random.default_rng(2).standard_normal((10, 3)), columns=["A", "B", "C"]
201-
)
202-
203-
data = np.random.default_rng(2).standard_normal(10)
204-
with tm.assert_produces_warning(FutureWarning):
205-
df1 = DataFrame.from_records(df, index=data)
206-
tm.assert_index_equal(df1.index, Index(data))
207-
208-
def test_from_records_bad_index_column(self):
209-
df = DataFrame(
210-
np.random.default_rng(2).standard_normal((10, 3)), columns=["A", "B", "C"]
211-
)
212-
213-
# should pass
214-
with tm.assert_produces_warning(FutureWarning):
215-
df1 = DataFrame.from_records(df, index=["C"])
216-
tm.assert_index_equal(df1.index, Index(df.C))
217-
218-
with tm.assert_produces_warning(FutureWarning):
219-
df1 = DataFrame.from_records(df, index="C")
220-
tm.assert_index_equal(df1.index, Index(df.C))
221-
222-
# should fail
223-
msg = "|".join(
224-
[
225-
r"'None of \[2\] are in the columns'",
226-
]
227-
)
228-
with pytest.raises(KeyError, match=msg):
229-
with tm.assert_produces_warning(FutureWarning):
230-
DataFrame.from_records(df, index=[2])
231-
with pytest.raises(KeyError, match=msg):
232-
with tm.assert_produces_warning(FutureWarning):
233-
DataFrame.from_records(df, index=2)
234-
235195
def test_from_records_non_tuple(self):
236196
class Record:
237197
def __init__(self, *args) -> None:

0 commit comments

Comments
 (0)