Skip to content

Commit a194a89

Browse files
committed
Make it easier to subclass Series and DataFrame by consistently using the _constructor attribute
1 parent 4c2d050 commit a194a89

File tree

2 files changed

+95
-80
lines changed

2 files changed

+95
-80
lines changed

pandas/core/frame.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
233233
return self._combine_series(casted, na_op, fill_value,
234234
axis, level)
235235
elif other.ndim == 2:
236-
casted = DataFrame(other, index=self.index,
236+
casted = self._constructor(other, index=self.index,
237237
columns=self.columns)
238238
return self._combine_frame(casted, na_op, fill_value, level)
239239
else: # pragma: no cover
@@ -297,7 +297,7 @@ def f(self, other, axis=default_axis, level=None):
297297
return self._combine_series(casted, na_op, None, axis, level)
298298

299299
elif other.ndim == 2:
300-
casted = DataFrame(other, index=self.index,
300+
casted = self._constructor(other, index=self.index,
301301
columns=self.columns)
302302

303303
return self._flex_compare_frame(casted, na_op, str_rep, level)
@@ -1771,7 +1771,7 @@ def as_blocks(self, columns=None):
17711771
bd = dict()
17721772
for b in self._data.blocks:
17731773
b = b.reindex_items_from(columns or b.items)
1774-
bd[str(b.dtype)] = DataFrame(BlockManager([ b ], [ b.items, self.index ]))
1774+
bd[str(b.dtype)] = self._constructor(BlockManager([ b ], [ b.items, self.index ]))
17751775
return bd
17761776

17771777
blocks = property(fget=as_blocks)
@@ -1841,12 +1841,12 @@ def _unpickle_matrix_compat(self, state): # pragma: no cover
18411841
(vals, idx, cols), object_state = state
18421842

18431843
index = _unpickle_array(idx)
1844-
dm = DataFrame(vals, index=index, columns=_unpickle_array(cols),
1844+
dm = self._constructor(vals, index=index, columns=_unpickle_array(cols),
18451845
copy=False)
18461846

18471847
if object_state is not None:
18481848
ovals, _, ocols = object_state
1849-
objects = DataFrame(ovals, index=index,
1849+
objects = self._constructor(ovals, index=index,
18501850
columns=_unpickle_array(ocols),
18511851
copy=False)
18521852

@@ -2041,7 +2041,7 @@ def _getitem_multilevel(self, key):
20412041
result.columns = result_columns
20422042
else:
20432043
new_values = self.values[:, loc]
2044-
result = DataFrame(new_values, index=self.index,
2044+
result = self._constructor(new_values, index=self.index,
20452045
columns=result_columns)
20462046
if len(result.columns) == 1:
20472047
top = result.columns[0]
@@ -2558,7 +2558,7 @@ def _align_series(self, other, join='outer', axis=None, level=None,
25582558
if copy and fdata is self._data:
25592559
fdata = fdata.copy()
25602560

2561-
left_result = DataFrame(fdata)
2561+
left_result = self._constructor(fdata)
25622562
right_result = other if ridx is None else other.reindex(join_index)
25632563

25642564
fill_na = notnull(fill_value) or (method is not None)
@@ -2737,7 +2737,7 @@ def _reindex_with_indexers(self, index, row_indexer, columns, col_indexer,
27372737
if copy and new_data is self._data:
27382738
new_data = new_data.copy()
27392739

2740-
return DataFrame(new_data)
2740+
return self._constructor(new_data)
27412741

27422742
def reindex_like(self, other, method=None, copy=True, limit=None,
27432743
fill_value=NA):
@@ -2985,7 +2985,7 @@ def take(self, indices, axis=0, convert=True):
29852985
if self._is_mixed_type:
29862986
if axis == 0:
29872987
new_data = self._data.take(indices, axis=1, verify=False)
2988-
return DataFrame(new_data)
2988+
return self._constructor(new_data)
29892989
else:
29902990
new_columns = self.columns.take(indices)
29912991
return self.reindex(columns=new_columns)
@@ -2999,7 +2999,7 @@ def take(self, indices, axis=0, convert=True):
29992999
else:
30003000
new_columns = self.columns.take(indices)
30013001
new_index = self.index
3002-
return DataFrame(new_values, index=new_index,
3002+
return self._constructor(new_values, index=new_index,
30033003
columns=new_columns)
30043004

30053005
#----------------------------------------------------------------------
@@ -4075,7 +4075,7 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
40754075
raise NotImplementedError
40764076

40774077
if not isinstance(other, DataFrame):
4078-
other = DataFrame(other)
4078+
other = self._constructor(other)
40794079

40804080
other = other.reindex_like(self)
40814081

@@ -4425,7 +4425,7 @@ def _apply_raw(self, func, axis):
44254425

44264426
# TODO: mixed type case
44274427
if result.ndim == 2:
4428-
return DataFrame(result, index=self.index,
4428+
return self._constructor(result, index=self.index,
44294429
columns=self.columns)
44304430
else:
44314431
return Series(result, index=self._get_agg_axis(axis))
@@ -4592,10 +4592,10 @@ def append(self, other, ignore_index=False, verify_integrity=False):
45924592

45934593
index = None if other.name is None else [other.name]
45944594
other = other.reindex(self.columns, copy=False)
4595-
other = DataFrame(other.values.reshape((1, len(other))),
4595+
other = self._constructor(other.values.reshape((1, len(other))),
45964596
index=index, columns=self.columns)
45974597
elif isinstance(other, list) and not isinstance(other[0], DataFrame):
4598-
other = DataFrame(other)
4598+
other = self._constructor(other)
45994599
if (self.columns.get_indexer(other.columns) >= 0).all():
46004600
other = other.ix[:, self.columns]
46014601

@@ -4660,7 +4660,7 @@ def _join_compat(self, other, on=None, how='left', lsuffix='', rsuffix='',
46604660
if isinstance(other, Series):
46614661
if other.name is None:
46624662
raise AssertionError('Other Series must have a name')
4663-
other = DataFrame({other.name: other})
4663+
other = self._constructor({other.name: other})
46644664

46654665
if isinstance(other, DataFrame):
46664666
return merge(self, other, left_on=on, how=how,
@@ -4862,7 +4862,7 @@ def describe(self, percentile_width=50):
48624862
numdata = self._get_numeric_data()
48634863

48644864
if len(numdata.columns) == 0:
4865-
return DataFrame(dict((k, v.describe())
4865+
return self._constructor(dict((k, v.describe())
48664866
for k, v in self.iteritems()),
48674867
columns=self.columns)
48684868

@@ -4954,7 +4954,7 @@ def _count_level(self, level, axis=0, numeric_only=False):
49544954
labels = com._ensure_int64(frame.index.labels[level])
49554955
counts = lib.count_level_2d(mask, labels, len(level_index))
49564956

4957-
result = DataFrame(counts, index=level_index,
4957+
result = self._constructor(counts, index=level_index,
49584958
columns=frame.columns)
49594959

49604960
if axis == 1:

0 commit comments

Comments
 (0)