Skip to content

Commit 1921c6f

Browse files
committed
Merge remote-tracking branch 'upstream/master' into combine-exception
2 parents 01f7366 + fea27f0 commit 1921c6f

35 files changed

+750
-302
lines changed

Diff for: ci/travis-27.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ dependencies:
4444
# universal
4545
- pytest
4646
- pytest-xdist
47-
- moto
47+
- moto==1.3.4
4848
- hypothesis>=3.58.0
4949
- pip:
5050
- backports.lzma

Diff for: doc/source/whatsnew/v0.24.0.txt

+30-2
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,35 @@ Current Behavior:
532532
...
533533
OverflowError: Trying to coerce negative values to unsigned integers
534534

535+
.. _whatsnew_0240.api.crosstab_dtypes
536+
537+
Crosstab Preserves Dtypes
538+
^^^^^^^^^^^^^^^^^^^^^^^^^
539+
540+
:func:`crosstab` will preserve now dtypes in some cases that previously would
541+
cast from integer dtype to floating dtype (:issue:`22019`)
542+
543+
Previous Behavior:
544+
545+
.. code-block:: ipython
546+
547+
In [3]: df = pd.DataFrame({'a': [1, 2, 2, 2, 2], 'b': [3, 3, 4, 4, 4],
548+
...: 'c': [1, 1, np.nan, 1, 1]})
549+
In [4]: pd.crosstab(df.a, df.b, normalize='columns')
550+
Out[4]:
551+
b 3 4
552+
a
553+
1 0.5 0.0
554+
2 0.5 1.0
555+
556+
Current Behavior:
557+
558+
.. code-block:: ipython
559+
560+
In [3]: df = pd.DataFrame({'a': [1, 2, 2, 2, 2], 'b': [3, 3, 4, 4, 4],
561+
...: 'c': [1, 1, np.nan, 1, 1]})
562+
In [4]: pd.crosstab(df.a, df.b, normalize='columns')
563+
535564
Datetimelike API Changes
536565
^^^^^^^^^^^^^^^^^^^^^^^^
537566

@@ -666,7 +695,7 @@ Timedelta
666695
- Bug in :class:`Index` with numeric dtype when multiplying or dividing an array with dtype ``timedelta64`` (:issue:`22390`)
667696
- Bug in :class:`TimedeltaIndex` incorrectly allowing indexing with ``Timestamp`` object (:issue:`20464`)
668697
- Fixed bug where subtracting :class:`Timedelta` from an object-dtyped array would raise ``TypeError`` (:issue:`21980`)
669-
-
698+
- Fixed bug in adding a :class:`DataFrame` with all-`timedelta64[ns]` dtypes to a :class:`DataFrame` with all-integer dtypes returning incorrect results instead of raising ``TypeError`` (:issue:`22696`)
670699
-
671700

672701
Timezones
@@ -834,4 +863,3 @@ Other
834863
- :meth:`DataFrame.nlargest` and :meth:`DataFrame.nsmallest` now returns the correct n values when keep != 'all' also when tied on the first columns (:issue:`22752`)
835864
- :meth:`~pandas.io.formats.style.Styler.bar` now also supports tablewise application (in addition to rowwise and columnwise) with ``axis=None`` and setting clipping range with ``vmin`` and ``vmax`` (:issue:`21548` and :issue:`21526`). ``NaN`` values are also handled properly.
836865
- Logical operations ``&, |, ^`` between :class:`Series` and :class:`Index` will no longer raise ``ValueError`` (:issue:`22092`)
837-
-

Diff for: pandas/core/arrays/interval.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,7 @@ class IntervalArray(IntervalMixin, ExtensionArray):
108108
_na_value = _fill_value = np.nan
109109

110110
def __new__(cls, data, closed=None, dtype=None, copy=False,
111-
fastpath=False, verify_integrity=True):
112-
113-
if fastpath:
114-
return cls._simple_new(data.left, data.right, closed,
115-
copy=copy, dtype=dtype,
116-
verify_integrity=False)
111+
verify_integrity=True):
117112

118113
if isinstance(data, ABCSeries) and is_interval_dtype(data):
119114
data = data.values

Diff for: pandas/core/arrays/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def asfreq(self, freq=None, how='E'):
264264
if self.hasnans:
265265
new_data[self._isnan] = iNaT
266266

267-
return self._simple_new(new_data, self.name, freq=freq)
267+
return self._shallow_copy(new_data, freq=freq)
268268

269269
# ------------------------------------------------------------------
270270
# Arithmetic Methods

Diff for: pandas/core/computation/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def visit_Subscript(self, node, **kwargs):
411411
slobj = self.visit(node.slice)
412412
try:
413413
value = value.value
414-
except:
414+
except AttributeError:
415415
pass
416416

417417
try:

Diff for: pandas/core/dtypes/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def is_timedelta64_dtype(arr_or_dtype):
467467
return False
468468
try:
469469
tipo = _get_dtype_type(arr_or_dtype)
470-
except:
470+
except (TypeError, ValueError, SyntaxError):
471471
return False
472472
return issubclass(tipo, np.timedelta64)
473473

Diff for: pandas/core/dtypes/dtypes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,11 @@ def construct_from_string(cls, string):
358358
try:
359359
if string == 'category':
360360
return cls()
361-
except:
361+
else:
362+
raise TypeError("cannot construct a CategoricalDtype")
363+
except AttributeError:
362364
pass
363365

364-
raise TypeError("cannot construct a CategoricalDtype")
365-
366366
@staticmethod
367367
def validate_ordered(ordered):
368368
"""
@@ -519,7 +519,7 @@ def __new__(cls, unit=None, tz=None):
519519
if m is not None:
520520
unit = m.groupdict()['unit']
521521
tz = m.groupdict()['tz']
522-
except:
522+
except TypeError:
523523
raise ValueError("could not construct DatetimeTZDtype")
524524

525525
elif isinstance(unit, compat.string_types):

Diff for: pandas/core/frame.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -3260,7 +3260,7 @@ def _ensure_valid_index(self, value):
32603260
if not len(self.index) and is_list_like(value):
32613261
try:
32623262
value = Series(value)
3263-
except:
3263+
except (ValueError, NotImplementedError, TypeError):
32643264
raise ValueError('Cannot set a frame with no defined index '
32653265
'and a value that cannot be converted to a '
32663266
'Series')
@@ -3629,7 +3629,8 @@ def align(self, other, join='outer', axis=None, level=None, copy=True,
36293629
fill_axis=fill_axis,
36303630
broadcast_axis=broadcast_axis)
36313631

3632-
@Appender(_shared_docs['reindex'] % _shared_doc_kwargs)
3632+
@Substitution(**_shared_doc_kwargs)
3633+
@Appender(NDFrame.reindex.__doc__)
36333634
@rewrite_axis_style_signature('labels', [('method', None),
36343635
('copy', True),
36353636
('level', None),
@@ -4479,7 +4480,8 @@ def f(vals):
44794480
# ----------------------------------------------------------------------
44804481
# Sorting
44814482

4482-
@Appender(_shared_docs['sort_values'] % _shared_doc_kwargs)
4483+
@Substitution(**_shared_doc_kwargs)
4484+
@Appender(NDFrame.sort_values.__doc__)
44834485
def sort_values(self, by, axis=0, ascending=True, inplace=False,
44844486
kind='quicksort', na_position='last'):
44854487
inplace = validate_bool_kwarg(inplace, 'inplace')
@@ -4521,7 +4523,8 @@ def sort_values(self, by, axis=0, ascending=True, inplace=False,
45214523
else:
45224524
return self._constructor(new_data).__finalize__(self)
45234525

4524-
@Appender(_shared_docs['sort_index'] % _shared_doc_kwargs)
4526+
@Substitution(**_shared_doc_kwargs)
4527+
@Appender(NDFrame.sort_index.__doc__)
45254528
def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
45264529
kind='quicksort', na_position='last', sort_remaining=True,
45274530
by=None):
@@ -4886,7 +4889,7 @@ def _arith_op(left, right):
48864889
left, right = ops.fill_binop(left, right, fill_value)
48874890
return func(left, right)
48884891

4889-
if this._is_mixed_type or other._is_mixed_type:
4892+
if ops.should_series_dispatch(this, other, func):
48904893
# iterate over columns
48914894
return ops.dispatch_to_series(this, other, _arith_op)
48924895
else:
@@ -4896,7 +4899,6 @@ def _arith_op(left, right):
48964899
copy=False)
48974900

48984901
def _combine_match_index(self, other, func, level=None):
4899-
assert isinstance(other, Series)
49004902
left, right = self.align(other, join='outer', axis=0, level=level,
49014903
copy=False)
49024904
assert left.index.equals(right.index)
@@ -4916,11 +4918,7 @@ def _combine_match_columns(self, other, func, level=None, try_cast=True):
49164918
left, right = self.align(other, join='outer', axis=1, level=level,
49174919
copy=False)
49184920
assert left.columns.equals(right.index)
4919-
4920-
new_data = left._data.eval(func=func, other=right,
4921-
axes=[left.columns, self.index],
4922-
try_cast=try_cast)
4923-
return self._constructor(new_data)
4921+
return ops.dispatch_to_series(left, right, func, axis="columns")
49244922

49254923
def _combine_const(self, other, func, errors='raise', try_cast=True):
49264924
if lib.is_scalar(other) or np.ndim(other) == 0:
@@ -7747,7 +7745,7 @@ def convert(v):
77477745
values = np.array([convert(v) for v in values])
77487746
else:
77497747
values = convert(values)
7750-
except:
7748+
except (ValueError, TypeError):
77517749
values = convert(values)
77527750

77537751
else:

0 commit comments

Comments
 (0)