Skip to content

Commit 51247f3

Browse files
committed
fix merge conflict
2 parents b8692f7 + ab74e77 commit 51247f3

File tree

8 files changed

+280
-172
lines changed

8 files changed

+280
-172
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Release notes
4646
* `Series` arithmetic methods with optional fill_value for missing data,
4747
e.g. a.add(b, fill_value=0). If a location is missing for both it will still
4848
be missing in the result though.
49+
* fill_value option has been added to `DataFrame`.{add, mul, sub, div} methods
50+
similar to `Series`
4951
* Boolean indexing with `DataFrame` objects: data[data > 0.1] = 0.1
5052
* `pytz` / tzinfo support in `DateRange`
5153
* `tz_localize`, `tz_normalize`, and `tz_validate` methods added

pandas/core/frame.py

Lines changed: 38 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
_try_sort, _pfixed, _default_index,
2626
_infer_dtype)
2727
from pandas.core.daterange import DateRange
28-
from pandas.core.generic import AxisProperty, PandasGeneric
28+
from pandas.core.generic import AxisProperty, NDFrame
2929
from pandas.core.index import Index, NULL_INDEX
3030
from pandas.core.internals import BlockManager, make_block
3131
from pandas.core.series import Series, _is_bool_indexer
@@ -94,7 +94,7 @@ def f(self, other):
9494
#-------------------------------------------------------------------------------
9595
# DataFrame class
9696

97-
class DataFrame(PandasGeneric):
97+
class DataFrame(NDFrame):
9898
"""
9999
Homogenously indexed table with named columns, with intelligent arithmetic
100100
operations, slicing, reindexing, aggregation, etc. Can function
@@ -217,11 +217,19 @@ def astype(self, dtype):
217217
-------
218218
casted : DataFrame
219219
"""
220-
return DataFrame(self._data, dtype=dtype)
220+
return type(self)(self._data, dtype=dtype)
221+
222+
def _wrap_array(self, arr, axes, copy=False):
223+
index, columns = axes
224+
return type(self)(arr, index=index, columns=columns, copy=copy)
225+
226+
@property
227+
def axes(self):
228+
return [self.index, self.columns]
221229

222230
@property
223231
def _constructor(self):
224-
return DataFrame
232+
return type(self)
225233

226234
#----------------------------------------------------------------------
227235
# Class behavior
@@ -269,7 +277,7 @@ def copy(self):
269277
"""
270278
Make a copy of this DataFrame
271279
"""
272-
return DataFrame(self._data.copy())
280+
return type(self)(self._data.copy())
273281

274282
#----------------------------------------------------------------------
275283
# Arithmetic methods
@@ -565,8 +573,8 @@ def transpose(self):
565573
Returns a DataFrame with the rows/columns switched. Copy of data is not
566574
made by default
567575
"""
568-
return DataFrame(data=self.values.T, index=self.columns,
569-
columns=self.index, copy=False)
576+
return type(self)(data=self.values.T, index=self.columns,
577+
columns=self.index, copy=False)
570578
T = property(transpose)
571579

572580
#----------------------------------------------------------------------
@@ -642,8 +650,8 @@ def __array__(self):
642650
return self.values
643651

644652
def __array_wrap__(self, result):
645-
return DataFrame(result, index=self.index, columns=self.columns,
646-
copy=False)
653+
return type(self)(result, index=self.index, columns=self.columns,
654+
copy=False)
647655

648656
#----------------------------------------------------------------------
649657
# getitem/setitem related
@@ -673,7 +681,7 @@ def __getitem__(self, item):
673681
"""
674682
if isinstance(item, slice):
675683
new_data = self._data.get_slice(item, axis=1)
676-
return DataFrame(new_data)
684+
return type(self)(new_data)
677685
elif isinstance(item, np.ndarray):
678686
if len(item) != len(self.index):
679687
raise ValueError('Item wrong length %d instead of %d!' %
@@ -837,11 +845,11 @@ def _reindex_index(self, new_index, method):
837845
if new_index is self.index:
838846
return self.copy()
839847
new_data = self._data.reindex_axis(new_index, method, axis=1)
840-
return DataFrame(new_data)
848+
return type(self)(new_data)
841849

842850
def _reindex_columns(self, new_columns):
843851
new_data = self._data.reindex_items(new_columns)
844-
return DataFrame(new_data)
852+
return type(self)(new_data)
845853

846854
def reindex_like(self, other, method=None):
847855
"""
@@ -1039,14 +1047,14 @@ def fillna(self, value=None, method='pad'):
10391047
series = self._series
10401048
for col, s in series.iteritems():
10411049
result[col] = s.fillna(method=method, value=value)
1042-
return DataFrame(result, index=self.index, columns=self.columns)
1050+
return type(self)(result, index=self.index, columns=self.columns)
10431051
else:
10441052
# Float type values
10451053
if len(self.columns) == 0:
10461054
return self
10471055

10481056
new_data = self._data.fillna(value)
1049-
return DataFrame(new_data, index=self.index, columns=self.columns)
1057+
return type(self)(new_data, index=self.index, columns=self.columns)
10501058

10511059
#----------------------------------------------------------------------
10521060
# Rename
@@ -1122,7 +1130,7 @@ def _combine_frame(self, other, func, fill_value=None):
11221130
# some shortcuts
11231131
if fill_value is None:
11241132
if not self and not other:
1125-
return DataFrame(index=new_index)
1133+
return type(self)(index=new_index)
11261134
elif not self:
11271135
return other * nan
11281136
elif not other:
@@ -1155,8 +1163,8 @@ def _combine_frame(self, other, func, fill_value=None):
11551163
other_vals[other_mask & mask] = fill_value
11561164

11571165
result = func(this_vals, other_vals)
1158-
return DataFrame(result, index=new_index, columns=new_columns,
1159-
copy=False)
1166+
return type(self)(result, index=new_index, columns=new_columns,
1167+
copy=False)
11601168

11611169
def _indexed_same(self, other):
11621170
same_index = self.index.equals(other.index)
@@ -1193,8 +1201,8 @@ def _combine_match_index(self, other, func, fill_value=None):
11931201
if fill_value is not None:
11941202
raise NotImplementedError
11951203

1196-
return DataFrame(func(values.T, other_vals).T, index=new_index,
1197-
columns=self.columns, copy=False)
1204+
return type(self)(func(values.T, other_vals).T, index=new_index,
1205+
columns=self.columns, copy=False)
11981206

11991207
def _combine_match_columns(self, other, func, fill_value=None):
12001208
newCols = self.columns.union(other.index)
@@ -1206,15 +1214,15 @@ def _combine_match_columns(self, other, func, fill_value=None):
12061214
if fill_value is not None:
12071215
raise NotImplementedError
12081216

1209-
return DataFrame(func(this.values, other), index=self.index,
1210-
columns=newCols, copy=False)
1217+
return type(self)(func(this.values, other), index=self.index,
1218+
columns=newCols, copy=False)
12111219

12121220
def _combine_const(self, other, func):
12131221
if not self:
12141222
return self
12151223

1216-
return DataFrame(func(self.values, other), index=self.index,
1217-
columns=self.columns, copy=False)
1224+
return type(self)(func(self.values, other), index=self.index,
1225+
columns=self.columns, copy=False)
12181226

12191227
def _compare_frame(self, other, func):
12201228
if not self._indexed_same(other):
@@ -1479,7 +1487,7 @@ def _shift_block(blk, indexer):
14791487
new_data = self._data.copy()
14801488
new_data.axes[1] = self.index.shift(periods, offset)
14811489

1482-
return DataFrame(new_data)
1490+
return type(self)(new_data)
14831491

14841492
def _shift_indexer(self, periods):
14851493
# small reusable utility
@@ -1526,8 +1534,8 @@ def apply(self, func, axis=0, broadcast=False):
15261534

15271535
if isinstance(func, np.ufunc):
15281536
results = func(self.values)
1529-
return DataFrame(data=results, index=self.index,
1530-
columns=self.columns, copy=False)
1537+
return type(self)(data=results, index=self.index,
1538+
columns=self.columns, copy=False)
15311539
else:
15321540
if not broadcast:
15331541
return self._apply_standard(func, axis)
@@ -1683,7 +1691,7 @@ def _join_on(self, other, on):
16831691
raise Exception('%s column not contained in this frame!' % on)
16841692

16851693
new_data = self._data.join_on(other._data, self[on], axis=1)
1686-
return DataFrame(new_data)
1694+
return type(self)(new_data)
16871695

16881696
def _join_index(self, other, how):
16891697
join_index = self._get_join_index(other, how)
@@ -1693,7 +1701,7 @@ def _join_index(self, other, how):
16931701
# merge blocks
16941702
merged_data = this_data.merge(other_data)
16951703
assert(merged_data.axes[1] is join_index) # maybe unnecessary
1696-
return DataFrame(merged_data)
1704+
return type(self)(merged_data)
16971705

16981706
def _get_join_index(self, other, how):
16991707
if how == 'left':
@@ -1788,7 +1796,7 @@ def corr(self):
17881796
correl[i, j] = c
17891797
correl[j, i] = c
17901798

1791-
return DataFrame(correl, index=cols, columns=cols)
1799+
return type(self)(correl, index=cols, columns=cols)
17921800

17931801
def corrwith(self, other, axis=0, drop=False):
17941802
"""
@@ -1858,7 +1866,7 @@ def describe(self):
18581866
tmp.quantile(.1), tmp.median(),
18591867
tmp.quantile(.9), tmp.max()]
18601868

1861-
return DataFrame(data, index=cols_destat, columns=cols)
1869+
return type(self)(data, index=cols_destat, columns=cols)
18621870

18631871
#----------------------------------------------------------------------
18641872
# ndarray-like stats methods
@@ -1939,31 +1947,6 @@ def sum(self, axis=0, numeric_only=False):
19391947

19401948
return Series(the_sum, index=axis_labels)
19411949

1942-
def cumsum(self, axis=0):
1943-
"""
1944-
Return DataFrame of cumulative sums over requested axis.
1945-
1946-
Parameters
1947-
----------
1948-
axis : {0, 1}
1949-
0 for row-wise, 1 for column-wise
1950-
1951-
Returns
1952-
-------
1953-
y : DataFrame
1954-
"""
1955-
y = np.array(self.values, subok=True)
1956-
if not issubclass(y.dtype.type, np.int_):
1957-
mask = np.isnan(self.values)
1958-
y[mask] = 0
1959-
result = y.cumsum(axis)
1960-
has_obs = (-mask).astype(int).cumsum(axis) > 0
1961-
result[-has_obs] = np.nan
1962-
else:
1963-
result = y.cumsum(axis)
1964-
return DataFrame(result, index=self.index, columns=self.columns,
1965-
copy=False)
1966-
19671950
def min(self, axis=0):
19681951
"""
19691952
Return array or Series of minimums over requested axis.
@@ -1998,30 +1981,6 @@ def max(self, axis=0):
19981981
np.putmask(values, -np.isfinite(values), -np.inf)
19991982
return Series(values.max(axis), index=self._get_agg_axis(axis))
20001983

2001-
def cumprod(self, axis=0):
2002-
"""
2003-
Return cumulative product over requested axis as DataFrame
2004-
2005-
Parameters
2006-
----------
2007-
axis : {0, 1}
2008-
0 for row-wise, 1 for column-wise
2009-
2010-
Returns
2011-
-------
2012-
y : DataFrame
2013-
"""
2014-
def get_cumprod(y):
2015-
y = np.array(y)
2016-
mask = isnull(y)
2017-
if not issubclass(y.dtype.type, np.int_):
2018-
y[mask] = 1
2019-
result = y.cumprod()
2020-
2021-
return result
2022-
2023-
return self.apply(get_cumprod, axis=axis)
2024-
20251984
def product(self, axis=0):
20261985
"""
20271986
Return array or Series of products over requested axis.

0 commit comments

Comments
 (0)