Skip to content

Commit d645a2a

Browse files
author
Matt Roeschke
committed
Merge branch 'master' into feature/generalized_window_operations
2 parents d812ae1 + 68a663b commit d645a2a

File tree

7 files changed

+33
-40
lines changed

7 files changed

+33
-40
lines changed

pandas/core/frame.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5269,7 +5269,7 @@ def _arith_op(left, right):
52695269
with np.errstate(all="ignore"):
52705270
res_values = _arith_op(this.values, other.values)
52715271
new_data = dispatch_fill_zeros(func, this.values, other.values, res_values)
5272-
return this._construct_result(other, new_data, _arith_op)
5272+
return this._construct_result(new_data)
52735273

52745274
def _combine_match_index(self, other, func, level=None):
52755275
left, right = self.align(other, join="outer", axis=0, level=level, copy=False)
@@ -5282,44 +5282,31 @@ def _combine_match_index(self, other, func, level=None):
52825282
# fastpath --> operate directly on values
52835283
with np.errstate(all="ignore"):
52845284
new_data = func(left.values.T, right.values).T
5285-
return left._construct_result(other, new_data, func)
5285+
return left._construct_result(new_data)
52865286

52875287
def _combine_match_columns(self, other: Series, func, level=None):
52885288
left, right = self.align(other, join="outer", axis=1, level=level, copy=False)
52895289
# at this point we have `left.columns.equals(right.index)`
52905290
new_data = ops.dispatch_to_series(left, right, func, axis="columns")
5291-
return left._construct_result(right, new_data, func)
5291+
return left._construct_result(new_data)
52925292

5293-
def _combine_const(self, other, func):
5294-
# scalar other or np.ndim(other) == 0
5295-
new_data = ops.dispatch_to_series(self, other, func)
5296-
return self._construct_result(other, new_data, func)
5297-
5298-
def _construct_result(self, other, result, func):
5293+
def _construct_result(self, result) -> "DataFrame":
52995294
"""
53005295
Wrap the result of an arithmetic, comparison, or logical operation.
53015296
53025297
Parameters
53035298
----------
5304-
other : object
53055299
result : DataFrame
5306-
func : binary operator
53075300
53085301
Returns
53095302
-------
53105303
DataFrame
5311-
5312-
Notes
5313-
-----
5314-
`func` is included for compat with SparseDataFrame signature, is not
5315-
needed here.
53165304
"""
53175305
out = self._constructor(result, index=self.index, copy=False)
53185306
# Pin columns instead of passing to constructor for compat with
53195307
# non-unique columns case
53205308
out.columns = self.columns
53215309
return out
5322-
# TODO: finalize? we do for SparseDataFrame
53235310

53245311
def combine(self, other, func, fill_value=None, overwrite=True):
53255312
"""

pandas/core/ops/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,8 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
892892
if fill_value is not None:
893893
self = self.fillna(fill_value)
894894

895-
return self._combine_const(other, op)
895+
new_data = dispatch_to_series(self, other, op)
896+
return self._construct_result(new_data)
896897

897898
f.__name__ = op_name
898899

@@ -926,15 +927,16 @@ def f(self, other, axis=default_axis, level=None):
926927
if not self._indexed_same(other):
927928
self, other = self.align(other, "outer", level=level, copy=False)
928929
new_data = dispatch_to_series(self, other, na_op, str_rep)
929-
return self._construct_result(other, new_data, na_op)
930+
return self._construct_result(new_data)
930931

931932
elif isinstance(other, ABCSeries):
932933
return _combine_series_frame(
933934
self, other, na_op, fill_value=None, axis=axis, level=level
934935
)
935936
else:
936937
# in this case we always have `np.ndim(other) == 0`
937-
return self._combine_const(other, na_op)
938+
new_data = dispatch_to_series(self, other, na_op)
939+
return self._construct_result(new_data)
938940

939941
f.__name__ = op_name
940942

@@ -957,7 +959,7 @@ def f(self, other):
957959
"Can only compare identically-labeled DataFrame objects"
958960
)
959961
new_data = dispatch_to_series(self, other, func, str_rep)
960-
return self._construct_result(other, new_data, func)
962+
return self._construct_result(new_data)
961963

962964
elif isinstance(other, ABCSeries):
963965
return _combine_series_frame(
@@ -967,8 +969,8 @@ def f(self, other):
967969

968970
# straight boolean comparisons we want to allow all columns
969971
# (regardless of dtype to pass thru) See #4537 for discussion.
970-
res = self._combine_const(other, func)
971-
return res
972+
new_data = dispatch_to_series(self, other, func)
973+
return self._construct_result(new_data)
972974

973975
f.__name__ = op_name
974976

pandas/core/series.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,9 @@ def _get_with(self, key):
11311131
elif isinstance(key, tuple):
11321132
try:
11331133
return self._get_values_tuple(key)
1134-
except Exception:
1134+
except ValueError:
1135+
# if we don't have a MultiIndex, we may still be able to handle
1136+
# a 1-tuple. see test_1tuple_without_multiindex
11351137
if len(key) == 1:
11361138
key = key[0]
11371139
if isinstance(key, slice):
@@ -1186,7 +1188,9 @@ def _get_values(self, indexer):
11861188
return self._constructor(
11871189
self._data.get_slice(indexer), fastpath=True
11881190
).__finalize__(self)
1189-
except Exception:
1191+
except ValueError:
1192+
# mpl compat if we look up e.g. ser[:, np.newaxis];
1193+
# see tests.series.timeseries.test_mpl_compat_hack
11901194
return self._values[indexer]
11911195

11921196
def _get_value(self, label, takeable: bool = False):

pandas/tests/indexing/test_iloc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pandas as pd
99
from pandas import DataFrame, Series, concat, date_range, isna
1010
from pandas.api.types import is_scalar
11+
from pandas.core.indexing import IndexingError
1112
from pandas.tests.indexing.common import Base
1213
from pandas.util import testing as tm
1314

@@ -722,7 +723,7 @@ def test_iloc_mask(self):
722723
else:
723724
accessor = df
724725
ans = str(bin(accessor[mask]["nums"].sum()))
725-
except Exception as e:
726+
except (ValueError, IndexingError, NotImplementedError) as e:
726727
ans = str(e)
727728

728729
key = tuple([idx, method])

pandas/tests/indexing/test_indexing.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,3 +1202,12 @@ def test_readonly_indices():
12021202
result = df["data"].iloc[indices]
12031203
expected = df["data"].loc[[1, 3, 6]]
12041204
tm.assert_series_equal(result, expected)
1205+
1206+
1207+
def test_1tuple_without_multiindex():
1208+
ser = pd.Series(range(5))
1209+
key = (slice(3),)
1210+
1211+
result = ser[key]
1212+
expected = ser[key[0]]
1213+
tm.assert_series_equal(result, expected)

pandas/tests/io/pytables/test_pytables.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import pandas.util.testing as tm
3838
from pandas.util.testing import assert_frame_equal, assert_series_equal, set_timezone
3939

40-
from pandas.io.formats.printing import pprint_thing
4140
from pandas.io.pytables import (
4241
ClosedFileError,
4342
HDFStore,
@@ -3415,14 +3414,9 @@ def test_string_select(self):
34153414
expected = df[df.x == "none"]
34163415
assert_frame_equal(result, expected)
34173416

3418-
try:
3419-
result = store.select("df", "x!=none")
3420-
expected = df[df.x != "none"]
3421-
assert_frame_equal(result, expected)
3422-
except Exception as detail:
3423-
pprint_thing("[{0}]".format(detail))
3424-
pprint_thing(store)
3425-
pprint_thing(expected)
3417+
result = store.select("df", "x!=none")
3418+
expected = df[df.x != "none"]
3419+
assert_frame_equal(result, expected)
34263420

34273421
df2 = df.copy()
34283422
df2.loc[df2.x == "", "x"] = np.nan

pandas/util/testing.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from shutil import rmtree
1010
import string
1111
import tempfile
12-
import traceback
1312
from typing import Union, cast
1413
import warnings
1514
import zipfile
@@ -2291,10 +2290,7 @@ def wrapper(*args, **kwargs):
22912290
" and error {error}".format(error=e)
22922291
)
22932292

2294-
try:
2295-
e_str = traceback.format_exc(e)
2296-
except Exception:
2297-
e_str = str(e)
2293+
e_str = str(e)
22982294

22992295
if any(m.lower() in e_str.lower() for m in _skip_on_messages):
23002296
skip(

0 commit comments

Comments
 (0)