Skip to content

Commit 9aa9e4f

Browse files
authored
CLN: NDFrame.__init__ unnecessary code (#32131)
1 parent a66e149 commit 9aa9e4f

File tree

4 files changed

+17
-53
lines changed

4 files changed

+17
-53
lines changed

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def __init__(
514514
else:
515515
raise ValueError("DataFrame constructor not properly called!")
516516

517-
NDFrame.__init__(self, mgr, fastpath=True)
517+
NDFrame.__init__(self, mgr)
518518

519519
# ----------------------------------------------------------------------
520520

pandas/core/generic.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
from pandas._libs import Timestamp, iNaT, lib
3434
from pandas._typing import (
3535
Axis,
36-
Dtype,
3736
FilePathOrBuffer,
3837
FrameOrSeries,
3938
JSONSerializable,
@@ -200,22 +199,10 @@ class NDFrame(PandasObject, SelectionMixin, indexing.IndexingMixin):
200199
def __init__(
201200
self,
202201
data: BlockManager,
203-
axes: Optional[List[Index]] = None,
204202
copy: bool = False,
205-
dtype: Optional[Dtype] = None,
206203
attrs: Optional[Mapping[Optional[Hashable], Any]] = None,
207-
fastpath: bool = False,
208204
):
209-
210-
if not fastpath:
211-
if dtype is not None:
212-
data = data.astype(dtype)
213-
elif copy:
214-
data = data.copy()
215-
216-
if axes is not None:
217-
for i, ax in enumerate(axes):
218-
data = data.reindex_axis(ax, axis=i)
205+
# copy kwarg is retained for mypy compat, is not used
219206

220207
object.__setattr__(self, "_is_copy", None)
221208
object.__setattr__(self, "_data", data)
@@ -226,12 +213,13 @@ def __init__(
226213
attrs = dict(attrs)
227214
object.__setattr__(self, "_attrs", attrs)
228215

229-
def _init_mgr(self, mgr, axes=None, dtype=None, copy=False):
216+
@classmethod
217+
def _init_mgr(cls, mgr, axes=None, dtype=None, copy=False):
230218
""" passed a manager and a axes dict """
231219
for a, axe in axes.items():
232220
if axe is not None:
233221
mgr = mgr.reindex_axis(
234-
axe, axis=self._get_block_manager_axis(a), copy=False
222+
axe, axis=cls._get_block_manager_axis(a), copy=False
235223
)
236224

237225
# make a copy if explicitly requested
@@ -262,7 +250,8 @@ def attrs(self) -> Dict[Optional[Hashable], Any]:
262250
def attrs(self, value: Mapping[Optional[Hashable], Any]) -> None:
263251
self._attrs = dict(value)
264252

265-
def _validate_dtype(self, dtype):
253+
@classmethod
254+
def _validate_dtype(cls, dtype):
266255
""" validate the passed dtype """
267256
if dtype is not None:
268257
dtype = pandas_dtype(dtype)
@@ -271,7 +260,7 @@ def _validate_dtype(self, dtype):
271260
if dtype.kind == "V":
272261
raise NotImplementedError(
273262
"compound dtypes are not implemented "
274-
f"in the {type(self).__name__} constructor"
263+
f"in the {cls.__name__} constructor"
275264
)
276265

277266
return dtype
@@ -324,8 +313,9 @@ def _construct_axes_dict(self, axes=None, **kwargs):
324313
d.update(kwargs)
325314
return d
326315

316+
@classmethod
327317
def _construct_axes_from_arguments(
328-
self, args, kwargs, require_all: bool = False, sentinel=None
318+
cls, args, kwargs, require_all: bool = False, sentinel=None
329319
):
330320
"""
331321
Construct and returns axes if supplied in args/kwargs.
@@ -339,7 +329,7 @@ def _construct_axes_from_arguments(
339329
"""
340330
# construct the args
341331
args = list(args)
342-
for a in self._AXIS_ORDERS:
332+
for a in cls._AXIS_ORDERS:
343333

344334
# look for a argument by position
345335
if a not in kwargs:
@@ -349,7 +339,7 @@ def _construct_axes_from_arguments(
349339
if require_all:
350340
raise TypeError("not enough/duplicate arguments specified!")
351341

352-
axes = {a: kwargs.pop(a, sentinel) for a in self._AXIS_ORDERS}
342+
axes = {a: kwargs.pop(a, sentinel) for a in cls._AXIS_ORDERS}
353343
return axes, kwargs
354344

355345
@classmethod
@@ -495,7 +485,7 @@ def ndim(self) -> int:
495485
return self._data.ndim
496486

497487
@property
498-
def size(self):
488+
def size(self) -> int:
499489
"""
500490
Return an int representing the number of elements in this object.
501491
@@ -3660,7 +3650,7 @@ def get(self, key, default=None):
36603650
return default
36613651

36623652
@property
3663-
def _is_view(self):
3653+
def _is_view(self) -> bool_t:
36643654
"""Return boolean indicating if self is view of another array """
36653655
return self._data.is_view
36663656

@@ -5176,12 +5166,12 @@ def _consolidate(self, inplace: bool_t = False):
51765166
return self._constructor(cons_data).__finalize__(self)
51775167

51785168
@property
5179-
def _is_mixed_type(self):
5169+
def _is_mixed_type(self) -> bool_t:
51805170
f = lambda: self._data.is_mixed_type
51815171
return self._protect_consolidate(f)
51825172

51835173
@property
5184-
def _is_numeric_mixed_type(self):
5174+
def _is_numeric_mixed_type(self) -> bool_t:
51855175
f = lambda: self._data.is_numeric_mixed_type
51865176
return self._protect_consolidate(f)
51875177

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def __init__(
324324

325325
data = SingleBlockManager(data, index, fastpath=True)
326326

327-
generic.NDFrame.__init__(self, data, fastpath=True)
327+
generic.NDFrame.__init__(self, data)
328328
self.name = name
329329
self._set_axis(0, index, fastpath=True)
330330

pandas/tests/indexing/test_indexing.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77
import numpy as np
88
import pytest
99

10-
from pandas.errors import AbstractMethodError
11-
1210
from pandas.core.dtypes.common import is_float_dtype, is_integer_dtype
1311

1412
import pandas as pd
1513
from pandas import DataFrame, Index, NaT, Series
1614
import pandas._testing as tm
17-
from pandas.core.generic import NDFrame
1815
from pandas.core.indexers import validate_indices
1916
from pandas.core.indexing import _maybe_numeric_slice, _non_reducing_slice
2017
from pandas.tests.indexing.common import _mklbl
@@ -1094,29 +1091,6 @@ def test_extension_array_cross_section_converts():
10941091
tm.assert_series_equal(result, expected)
10951092

10961093

1097-
@pytest.mark.parametrize(
1098-
"idxr, error, error_message",
1099-
[
1100-
(lambda x: x, AbstractMethodError, None),
1101-
(
1102-
lambda x: x.loc,
1103-
AttributeError,
1104-
"type object 'NDFrame' has no attribute '_AXIS_NAMES'",
1105-
),
1106-
(
1107-
lambda x: x.iloc,
1108-
AttributeError,
1109-
"type object 'NDFrame' has no attribute '_AXIS_NAMES'",
1110-
),
1111-
],
1112-
)
1113-
def test_ndframe_indexing_raises(idxr, error, error_message):
1114-
# GH 25567
1115-
frame = NDFrame(np.random.randint(5, size=(2, 2, 2)))
1116-
with pytest.raises(error, match=error_message):
1117-
idxr(frame)[0]
1118-
1119-
11201094
def test_readonly_indices():
11211095
# GH#17192 iloc with read-only array raising TypeError
11221096
df = pd.DataFrame({"data": np.ones(100, dtype="float64")})

0 commit comments

Comments
 (0)