Skip to content

Commit 3e78edb

Browse files
committed
ENH: replace some usages of DataFrame to more generic self._constructor. #2865
1 parent ed9f522 commit 3e78edb

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

pandas/core/frame.py

+27-22
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ def _init_ndarray(self, values, index, columns, dtype=None,
556556
columns = _ensure_index(columns)
557557

558558
return create_block_manager_from_blocks([ values.T ], [ columns, index ])
559-
559+
560560
def _wrap_array(self, arr, axes, copy=False):
561561
index, columns = axes
562562
return self._constructor(arr, index=index, columns=columns, copy=copy)
@@ -583,7 +583,7 @@ def axes(self):
583583

584584
@property
585585
def _constructor(self):
586-
return DataFrame
586+
return self.__class__
587587

588588
@property
589589
def shape(self):
@@ -865,15 +865,15 @@ def dot(self, other):
865865
(lvals.shape, rvals.shape))
866866

867867
if isinstance(other, DataFrame):
868-
return DataFrame(np.dot(lvals, rvals),
869-
index=left.index,
870-
columns=other.columns)
868+
return self._constructor(np.dot(lvals, rvals),
869+
index=left.index,
870+
columns=other.columns)
871871
elif isinstance(other, Series):
872872
return Series(np.dot(lvals, rvals), index=left.index)
873873
elif isinstance(rvals, np.ndarray):
874874
result = np.dot(lvals, rvals)
875875
if result.ndim == 2:
876-
return DataFrame(result, index=left.index)
876+
return self._constructor(result, index=left.index)
877877
else:
878878
return Series(result, index=left.index)
879879
else: # pragma: no cover
@@ -912,7 +912,7 @@ def from_dict(cls, data, orient='columns', dtype=None):
912912
elif orient != 'columns': # pragma: no cover
913913
raise ValueError('only recognize index or columns for orient')
914914

915-
return DataFrame(data, index=index, columns=columns, dtype=dtype)
915+
return cls(data, index=index, columns=columns, dtype=dtype)
916916

917917
def to_dict(self, outtype='dict'):
918918
"""
@@ -979,15 +979,15 @@ def from_records(cls, data, index=None, exclude=None, columns=None,
979979

980980
if com.is_iterator(data):
981981
if nrows == 0:
982-
return DataFrame()
982+
return cls()
983983

984984
try:
985985
if py3compat.PY3:
986986
first_row = next(data)
987987
else:
988988
first_row = data.next()
989989
except StopIteration:
990-
return DataFrame(index=index, columns=columns)
990+
return cls(index=index, columns=columns)
991991

992992
dtype = None
993993
if hasattr(first_row, 'dtype') and first_row.dtype.names:
@@ -1077,7 +1077,7 @@ def from_records(cls, data, index=None, exclude=None, columns=None,
10771077
mgr = _arrays_to_mgr(arrays, arr_columns, result_index,
10781078
columns)
10791079

1080-
return DataFrame(mgr)
1080+
return cls(mgr)
10811081

10821082
def to_records(self, index=True, convert_datetime64=True):
10831083
"""
@@ -1815,7 +1815,7 @@ def icol(self, i):
18151815
return self._ixs(i,axis=1)
18161816

18171817
def _ixs(self, i, axis=0, copy=False):
1818-
"""
1818+
"""
18191819
i : int, slice, or sequence of integers
18201820
axis : int
18211821
"""
@@ -1846,7 +1846,7 @@ def _ixs(self, i, axis=0, copy=False):
18461846
# icol
18471847
else:
18481848

1849-
"""
1849+
"""
18501850
Notes
18511851
-----
18521852
If slice passed, the resulting data will be a view
@@ -1954,7 +1954,7 @@ def _slice(self, slobj, axis=0, raise_on_error=False):
19541954
def _box_item_values(self, key, values):
19551955
items = self.columns[self.columns.get_loc(key)]
19561956
if values.ndim == 2:
1957-
return DataFrame(values.T, columns=items, index=self.index)
1957+
return self._constructor(values.T, columns=items, index=self.index)
19581958
else:
19591959
return Series.from_array(values, index=self.index, name=items)
19601960

@@ -2125,7 +2125,7 @@ def pop(self, item):
21252125
column : Series
21262126
"""
21272127
return NDFrame.pop(self, item)
2128-
2128+
21292129
# to support old APIs
21302130
@property
21312131
def _series(self):
@@ -2550,7 +2550,8 @@ def _reindex_multi(self, new_index, new_columns, copy, fill_value):
25502550
indexer = row_indexer, col_indexer
25512551
new_values = com.take_2d_multi(self.values, indexer,
25522552
fill_value=fill_value)
2553-
return DataFrame(new_values, index=new_index, columns=new_columns)
2553+
return self._constructor(new_values, index=new_index,
2554+
columns=new_columns)
25542555
elif row_indexer is not None:
25552556
return self._reindex_with_indexers(new_index, row_indexer,
25562557
None, None, copy, fill_value)
@@ -5060,7 +5061,8 @@ def rank(self, axis=0, numeric_only=None, method='average',
50605061
try:
50615062
ranks = algos.rank(self.values, axis=axis, method=method,
50625063
ascending=ascending, na_option=na_option)
5063-
return DataFrame(ranks, index=self.index, columns=self.columns)
5064+
return self._constructor(ranks, index=self.index,
5065+
columns=self.columns)
50645066
except TypeError:
50655067
numeric_only = True
50665068

@@ -5070,7 +5072,7 @@ def rank(self, axis=0, numeric_only=None, method='average',
50705072
data = self
50715073
ranks = algos.rank(data.values, axis=axis, method=method,
50725074
ascending=ascending, na_option=na_option)
5073-
return DataFrame(ranks, index=data.index, columns=data.columns)
5075+
return self._constructor(ranks, index=data.index, columns=data.columns)
50745076

50755077
def to_timestamp(self, freq=None, how='start', axis=0, copy=True):
50765078
"""
@@ -5104,7 +5106,7 @@ def to_timestamp(self, freq=None, how='start', axis=0, copy=True):
51045106
else:
51055107
raise ValueError('Axis must be 0 or 1. Got %s' % str(axis))
51065108

5107-
return DataFrame(new_data)
5109+
return self._constructor(new_data)
51085110

51095111
def to_period(self, freq=None, axis=0, copy=True):
51105112
"""
@@ -5139,7 +5141,7 @@ def to_period(self, freq=None, axis=0, copy=True):
51395141
else:
51405142
raise ValueError('Axis must be 0 or 1. Got %s' % str(axis))
51415143

5142-
return DataFrame(new_data)
5144+
return self._constructor(new_data)
51435145

51445146
#----------------------------------------------------------------------
51455147
# Deprecated stuff
@@ -5221,14 +5223,17 @@ def where(self, cond, other=NA, inplace=False, try_cast=False, raise_on_error=Tr
52215223
if other.shape != self.shape:
52225224
raise ValueError('other must be the same shape as self '
52235225
'when an ndarray')
5224-
other = DataFrame(other, self.index, self.columns)
5226+
other = self._constructor(other, self.index, self.columns)
52255227

52265228
if inplace:
5227-
# we may have different type blocks come out of putmask, so reconstruct the block manager
5229+
# we may have different type blocks come out of putmask, so
5230+
# reconstruct the block manager
52285231
self._data = self._data.putmask(cond,other,inplace=True)
52295232

52305233
else:
5231-
new_data = self._data.where(other, cond, raise_on_error=raise_on_error, try_cast=try_cast)
5234+
new_data = self._data.where(other, cond,
5235+
raise_on_error=raise_on_error,
5236+
try_cast=try_cast)
52325237

52335238
return self._constructor(new_data)
52345239

0 commit comments

Comments
 (0)