diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ecf2f8ba482f6..70b5a4ea6def9 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -565,7 +565,7 @@ def axes(self): @property def _constructor(self): - return DataFrame + return self.__class__ # Fancy indexing _ix = None @@ -855,7 +855,7 @@ def dot(self, other): (lvals.shape, rvals.shape)) if isinstance(other, DataFrame): - return DataFrame(np.dot(lvals, rvals), + return self._constructor(np.dot(lvals, rvals), index=left.index, columns=other.columns) elif isinstance(other, Series): @@ -863,7 +863,7 @@ def dot(self, other): elif isinstance(rvals, np.ndarray): result = np.dot(lvals, rvals) if result.ndim == 2: - return DataFrame(result, index=left.index) + return self._constructor(result, index=left.index) else: return Series(result, index=left.index) else: # pragma: no cover @@ -902,7 +902,7 @@ def from_dict(cls, data, orient='columns', dtype=None): elif orient != 'columns': # pragma: no cover raise ValueError('only recognize index or columns for orient') - return DataFrame(data, index=index, columns=columns, dtype=dtype) + return cls(data, index=index, columns=columns, dtype=dtype) def to_dict(self, outtype='dict'): """ @@ -969,7 +969,7 @@ def from_records(cls, data, index=None, exclude=None, columns=None, if com.is_iterator(data): if nrows == 0: - return DataFrame() + return cls() try: if py3compat.PY3: @@ -977,7 +977,7 @@ def from_records(cls, data, index=None, exclude=None, columns=None, else: first_row = data.next() except StopIteration: - return DataFrame(index=index, columns=columns) + return cls(index=index, columns=columns) dtype = None if hasattr(first_row, 'dtype') and first_row.dtype.names: @@ -1067,7 +1067,7 @@ def from_records(cls, data, index=None, exclude=None, columns=None, mgr = _arrays_to_mgr(arrays, arr_columns, result_index, columns) - return DataFrame(mgr) + return cls(mgr) def to_records(self, index=True, convert_datetime64=True): """ @@ -2061,7 +2061,7 @@ def _slice(self, slobj, axis=0): def _box_item_values(self, key, values): items = self.columns[self.columns.get_loc(key)] if values.ndim == 2: - return DataFrame(values.T, columns=items, index=self.index) + return self._constructor(values.T, columns=items, index=self.index) else: return Series.from_array(values, index=self.index, name=items) @@ -2647,7 +2647,7 @@ def _reindex_multi(self, new_index, new_columns, copy, fill_value): if row_indexer is not None and col_indexer is not None: new_values = com.take_2d_multi(self.values, row_indexer, col_indexer, fill_value=fill_value) - return DataFrame(new_values, index=new_index, columns=new_columns) + return self._constructor(new_values, index=new_index, columns=new_columns) elif row_indexer is not None: return self._reindex_with_indexers(new_index, row_indexer, None, None, copy, fill_value) @@ -2695,7 +2695,7 @@ def _reindex_with_indexers(self, index, row_indexer, columns, col_indexer, if copy and new_data is self._data: new_data = new_data.copy() - return DataFrame(new_data) + return self._constructor(new_data) def reindex_like(self, other, method=None, copy=True, limit=None): """ @@ -2938,7 +2938,7 @@ def take(self, indices, axis=0): if self._is_mixed_type: if axis == 0: new_data = self._data.take(indices, axis=1) - return DataFrame(new_data) + return self._constructor(new_data) else: new_columns = self.columns.take(indices) return self.reindex(columns=new_columns) @@ -2952,7 +2952,7 @@ def take(self, indices, axis=0): else: new_columns = self.columns.take(indices) new_index = self.index - return DataFrame(new_values, index=new_index, + return self._constructor(new_values, index=new_index, columns=new_columns) #---------------------------------------------------------------------- @@ -4191,7 +4191,7 @@ def _apply_raw(self, func, axis): # TODO: mixed type case if result.ndim == 2: - return DataFrame(result, index=self.index, + return self._constructor(result, index=self.index, columns=self.columns) else: return Series(result, index=self._get_agg_axis(axis)) @@ -4622,7 +4622,7 @@ def describe(self, percentile_width=50): numdata = self._get_numeric_data() if len(numdata.columns) == 0: - return DataFrame(dict((k, v.describe()) + return self._constructor(dict((k, v.describe()) for k, v in self.iteritems()), columns=self.columns) @@ -5006,7 +5006,7 @@ def _get_agg_axis(self, axis_num): def _get_numeric_data(self): if self._is_mixed_type: num_data = self._data.get_numeric_data() - return DataFrame(num_data, index=self.index, copy=False) + return self._constructor(num_data, index=self.index, copy=False) else: if (self.values.dtype != np.object_ and not issubclass(self.values.dtype.type, np.datetime64)): @@ -5017,7 +5017,7 @@ def _get_numeric_data(self): def _get_bool_data(self): if self._is_mixed_type: bool_data = self._data.get_bool_data() - return DataFrame(bool_data, index=self.index, copy=False) + return self._constructor(bool_data, index=self.index, copy=False) else: # pragma: no cover if self.values.dtype == np.bool_: return self @@ -5127,7 +5127,7 @@ def rank(self, axis=0, numeric_only=None, method='average', try: ranks = algos.rank(self.values, axis=axis, method=method, ascending=ascending, na_option=na_option) - return DataFrame(ranks, index=self.index, columns=self.columns) + return self._constructor(ranks, index=self.index, columns=self.columns) except TypeError: numeric_only = True @@ -5137,7 +5137,7 @@ def rank(self, axis=0, numeric_only=None, method='average', data = self ranks = algos.rank(data.values, axis=axis, method=method, ascending=ascending, na_option=na_option) - return DataFrame(ranks, index=data.index, columns=data.columns) + return self._constructor(ranks, index=data.index, columns=data.columns) def to_timestamp(self, freq=None, how='start', axis=0, copy=True): """ @@ -5170,7 +5170,7 @@ def to_timestamp(self, freq=None, how='start', axis=0, copy=True): else: raise ValueError('Axis must be 0 or 1. Got %s' % str(axis)) - return DataFrame(new_data) + return self._constructor(new_data) def to_period(self, freq=None, axis=0, copy=True): """ @@ -5204,7 +5204,7 @@ def to_period(self, freq=None, axis=0, copy=True): else: raise ValueError('Axis must be 0 or 1. Got %s' % str(axis)) - return DataFrame(new_data) + return self._constructor(new_data) #---------------------------------------------------------------------- # Deprecated stuff