Skip to content
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

DEPR: remove Series.from_array, DataFrame.from_items, as_matrix, asobject, as_blocks, blocks #29720

Merged
merged 5 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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: 0 additions & 1 deletion doc/source/reference/frame.rst
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ Serialization / IO / conversion
:toctree: api/

DataFrame.from_dict
DataFrame.from_items
DataFrame.from_records
DataFrame.info
DataFrame.to_parquet
Expand Down
7 changes: 6 additions & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,12 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
- Removed the previously deprecated ``reduce`` and ``broadcast`` arguments from :meth:`DataFrame.apply` (:issue:`18577`)
- Removed the previously deprecated ``assert_raises_regex`` function in ``pandas.util.testing`` (:issue:`29174`)
- Removed :meth:`Index.is_lexsorted_for_tuple` (:issue:`29305`)
- Removed support for nexted renaming in :meth:`DataFrame.aggregate`, :meth:`Series.aggregate`, :meth:`DataFrameGroupBy.aggregate`, :meth:`SeriesGroupBy.aggregate`, :meth:`Rolling.aggregate` (:issue:`29608`)
- Removed support for nested renaming in :meth:`DataFrame.aggregate`, :meth:`Series.aggregate`, :meth:`DataFrameGroupBy.aggregate`, :meth:`SeriesGroupBy.aggregate`, :meth:`Rolling.aggregate` (:issue:`18529`)
- Removed :meth:`Series.from_array` (:issue:`18258`)
- Removed :meth:`DataFrame.from_items` (:issue:`18458`)
- Removed :meth:`DataFrame.as_matrix`, :meth:`Series.as_matrix` (:issue:`18458`)
- Removed :meth:`Series.asobject` (:issue:`18477`)
- Removed :meth:`DataFrame.as_blocks`, :meth:`Series.as_blocks`, `DataFrame.blocks`, :meth:`Series.blocks` (:issue:`17656`)
-

.. _whatsnew_1000.performance:
Expand Down
107 changes: 4 additions & 103 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
is_iterator,
is_list_like,
is_named_tuple,
is_nested_list_like,
is_object_dtype,
is_scalar,
is_sequence,
Expand Down Expand Up @@ -343,8 +342,9 @@ class DataFrame(NDFrame):
--------
DataFrame.from_records : Constructor from tuples, also record arrays.
DataFrame.from_dict : From dicts of Series, arrays, or dicts.
DataFrame.from_items : From sequence of (key, value) pairs
read_csv, pandas.read_table, pandas.read_clipboard.
read_csv
read_table
read_clipboard

Examples
--------
Expand Down Expand Up @@ -388,9 +388,7 @@ def _constructor(self) -> Type["DataFrame"]:
return DataFrame

_constructor_sliced = Series # type: Type[Series]
_deprecations = NDFrame._deprecations | frozenset(
["from_items"]
) # type: FrozenSet[str]
_deprecations = NDFrame._deprecations | frozenset([]) # type: FrozenSet[str]
_accessors = set() # type: Set[str]

@property
Expand Down Expand Up @@ -1870,103 +1868,6 @@ def to_records(

return np.rec.fromarrays(arrays, dtype={"names": names, "formats": formats})

@classmethod
def from_items(cls, items, columns=None, orient="columns"):
"""
Construct a DataFrame from a list of tuples.

.. deprecated:: 0.23.0
`from_items` is deprecated and will be removed in a future version.
Use :meth:`DataFrame.from_dict(dict(items)) <DataFrame.from_dict>`
instead.
:meth:`DataFrame.from_dict(OrderedDict(items)) <DataFrame.from_dict>`
may be used to preserve the key order.

Convert (key, value) pairs to DataFrame. The keys will be the axis
index (usually the columns, but depends on the specified
orientation). The values should be arrays or Series.

Parameters
----------
items : sequence of (key, value) pairs
Values should be arrays or Series.
columns : sequence of column labels, optional
Must be passed if orient='index'.
orient : {'columns', 'index'}, default 'columns'
The "orientation" of the data. If the keys of the
input correspond to column labels, pass 'columns'
(default). Otherwise if the keys correspond to the index,
pass 'index'.

Returns
-------
DataFrame
"""

warnings.warn(
"from_items is deprecated. Please use "
"DataFrame.from_dict(dict(items), ...) instead. "
"DataFrame.from_dict(OrderedDict(items)) may be used to "
"preserve the key order.",
FutureWarning,
stacklevel=2,
)

keys, values = zip(*items)

if orient == "columns":
if columns is not None:
columns = ensure_index(columns)

idict = dict(items)
if len(idict) < len(items):
if not columns.equals(ensure_index(keys)):
raise ValueError(
"With non-unique item names, passed "
"columns must be identical"
)
arrays = values
else:
arrays = [idict[k] for k in columns if k in idict]
else:
columns = ensure_index(keys)
arrays = values

# GH 17312
# Provide more informative error msg when scalar values passed
try:
return cls._from_arrays(arrays, columns, None)

except ValueError:
if not is_nested_list_like(values):
raise ValueError(
"The value in each (key, value) pair "
"must be an array, Series, or dict"
)

elif orient == "index":
if columns is None:
raise TypeError("Must pass columns with orient='index'")

keys = ensure_index(keys)

# GH 17312
# Provide more informative error msg when scalar values passed
try:
arr = np.array(values, dtype=object).T
data = [lib.maybe_convert_objects(v) for v in arr]
return cls._from_arrays(data, columns, keys)

except TypeError:
if not is_nested_list_like(values):
raise ValueError(
"The value in each (key, value) pair "
"must be an array, Series, or dict"
)

else: # pragma: no cover
raise ValueError("'orient' must be either 'columns' or 'index'")

@classmethod
def _from_arrays(cls, arrays, columns, index, dtype=None):
mgr = arrays_to_mgr(arrays, columns, index, columns, dtype=dtype)
Expand Down
85 changes: 0 additions & 85 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ class NDFrame(PandasObject, SelectionMixin):
_accessors = set() # type: Set[str]
_deprecations = frozenset(
[
"as_blocks",
"as_matrix",
"blocks",
"clip_lower",
"clip_upper",
"get_dtype_counts",
Expand Down Expand Up @@ -5408,54 +5405,6 @@ def _get_bool_data(self):
# ----------------------------------------------------------------------
# Internal Interface Methods

def as_matrix(self, columns=None):
"""
Convert the frame to its Numpy-array representation.

.. deprecated:: 0.23.0
Use :meth:`DataFrame.values` instead.

Parameters
----------
columns : list, optional, default:None
If None, return all columns, otherwise, returns specified columns.

Returns
-------
values : ndarray
If the caller is heterogeneous and contains booleans or objects,
the result will be of dtype=object. See Notes.

See Also
--------
DataFrame.values

Notes
-----
Return is NOT a Numpy-matrix, rather, a Numpy-array.

The dtype will be a lower-common-denominator dtype (implicit
upcasting); that is to say if the dtypes (even of numeric types)
are mixed, the one that accommodates all will be chosen. Use this
with care if you are not dealing with the blocks.

e.g. If the dtypes are float16 and float32, dtype will be upcast to
float32. If dtypes are int32 and uint8, dtype will be upcase to
int32. By numpy.find_common_type convention, mixing int64 and uint64
will result in a float64 dtype.

This method is provided for backwards compatibility. Generally,
it is recommended to use '.values'.
"""
warnings.warn(
"Method .as_matrix will be removed in a future version. "
"Use .values instead.",
FutureWarning,
stacklevel=2,
)
self._consolidate_inplace()
return self._data.as_array(transpose=self._AXIS_REVERSED, items=columns)

@property
def values(self):
"""
Expand Down Expand Up @@ -5773,40 +5722,6 @@ def ftypes(self):

return Series(self._data.get_ftypes(), index=self._info_axis, dtype=np.object_)

def as_blocks(self, copy=True):
"""
Convert the frame to a dict of dtype -> Constructor Types.

.. deprecated:: 0.21.0

NOTE: the dtypes of the blocks WILL BE PRESERVED HERE (unlike in
as_matrix)

Parameters
----------
copy : bool, default True

Returns
-------
dict
Mapping dtype -> Constructor Types.
"""
warnings.warn(
"as_blocks is deprecated and will be removed in a future version",
FutureWarning,
stacklevel=2,
)
return self._to_dict_of_blocks(copy=copy)

@property
def blocks(self):
"""
Internal property, property synonym for as_blocks().

.. deprecated:: 0.21.0
"""
return self.as_blocks()

def _to_dict_of_blocks(self, copy=True):
"""
Return a dict of dtype -> Constructor Types that
Expand Down
56 changes: 1 addition & 55 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
base.IndexOpsMixin._deprecations
| generic.NDFrame._deprecations
| frozenset(
[
"asobject",
"compress",
"valid",
"ftype",
"real",
"imag",
"put",
"ptp",
"nonzero",
]
["compress", "valid", "ftype", "real", "imag", "put", "ptp", "nonzero"]
)
)

Expand Down Expand Up @@ -364,32 +354,6 @@ def _init_dict(self, data, index=None, dtype=None):
s = s.reindex(index, copy=False)
return s._data, s.index

@classmethod
def from_array(
cls, arr, index=None, name=None, dtype=None, copy=False, fastpath=False
):
"""
Construct Series from array.

.. deprecated:: 0.23.0
Use pd.Series(..) constructor instead.

Returns
-------
Series
Constructed Series.
"""
warnings.warn(
"'from_array' is deprecated and will be removed in a "
"future version. Please use the pd.Series(..) "
"constructor instead.",
FutureWarning,
stacklevel=2,
)
return cls(
arr, index=index, name=name, dtype=dtype, copy=copy, fastpath=fastpath
)

# ----------------------------------------------------------------------

@property
Expand Down Expand Up @@ -579,24 +543,6 @@ def get_values(self):
def _internal_get_values(self):
return self._data.get_values()

@property
def asobject(self):
"""
Return object Series which contains boxed values.

.. deprecated:: 0.23.0

Use ``astype(object)`` instead.

*this is an internal non-public method*
"""
warnings.warn(
"'asobject' is deprecated. Use 'astype(object)' instead",
FutureWarning,
stacklevel=2,
)
return self.astype(object).values

# ops
def ravel(self, order="C"):
"""
Expand Down
8 changes: 0 additions & 8 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,14 +476,6 @@ def test_values(self, float_frame):
float_frame.values[:, 0] = 5.0
assert (float_frame.values[:, 0] == 5).all()

def test_as_matrix_deprecated(self, float_frame):
# GH 18458
with tm.assert_produces_warning(FutureWarning):
cols = float_frame.columns.tolist()
result = float_frame.as_matrix(columns=cols)
expected = float_frame.values
tm.assert_numpy_array_equal(result, expected)

def test_deepcopy(self, float_frame):
cp = deepcopy(float_frame)
series = cp["A"]
Expand Down
10 changes: 2 additions & 8 deletions pandas/tests/frame/test_block_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,7 @@ def test_copy_blocks(self, float_frame):
column = df.columns[0]

# use the default copy=True, change a column

# deprecated 0.21.0
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
blocks = df.as_blocks()
blocks = df._to_dict_of_blocks(copy=True)
for dtype, _df in blocks.items():
if column in _df:
_df.loc[:, column] = _df[column] + 1
Expand All @@ -330,10 +327,7 @@ def test_no_copy_blocks(self, float_frame):
column = df.columns[0]

# use the copy=False, change a column

# deprecated 0.21.0
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
blocks = df.as_blocks(copy=False)
blocks = df._to_dict_of_blocks(copy=False)
for dtype, _df in blocks.items():
if column in _df:
_df.loc[:, column] = _df[column] + 1
Expand Down
Loading