Skip to content

Commit 645cf93

Browse files
jrebackjorisvandenbossche
authored andcommitted
[Backport pandas-dev#14684] ERR: more informative message on invalid Timestamp input
TST: fix unordable error message xref pandas-dev#14679 TST: handle unorderable exceptions in indexing closes pandas-dev#14684 (cherry picked from commit b6ffd89)
1 parent d7b1661 commit 645cf93

File tree

7 files changed

+46
-8
lines changed

7 files changed

+46
-8
lines changed

doc/source/whatsnew/v0.19.2.txt

+6
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,11 @@ Bug Fixes
4141
- Bug in not propogating exceptions in parsing invalid datetimes, noted in python 3.6 (:issue:`14561`)
4242

4343

44+
- Compat with python 3.6 for pickling of some offsets (:issue:`14685`)
45+
- Compat with python 3.6 for some indexing exception types (:issue:`14684`)
46+
- Compat with python 3.6 for deprecation warnings in the test suite (:issue:`14681`)
47+
48+
49+
4450

4551
- Explicit check in ``to_stata`` and ``StataWriter`` for out-of-range values when writing doubles (:issue:`14618`)

pandas/compat/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
PY2 = sys.version_info[0] == 2
4242
PY3 = (sys.version_info[0] >= 3)
4343
PY35 = (sys.version_info >= (3, 5))
44+
PY36 = (sys.version_info >= (3, 6))
4445

4546
try:
4647
import __builtin__ as builtins

pandas/core/indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
is_sequence,
1212
is_scalar,
1313
is_sparse,
14+
_is_unorderable_exception,
1415
_ensure_platform_int)
1516
from pandas.types.missing import isnull, _infer_fill_value
1617

@@ -1411,7 +1412,7 @@ def error():
14111412
except TypeError as e:
14121413

14131414
# python 3 type errors should be raised
1414-
if 'unorderable' in str(e): # pragma: no cover
1415+
if _is_unorderable_exception(e):
14151416
error()
14161417
raise
14171418
except:

pandas/core/series.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
is_iterator,
2626
is_dict_like,
2727
is_scalar,
28+
_is_unorderable_exception,
2829
_ensure_platform_int)
2930
from pandas.types.generic import ABCSparseArray, ABCDataFrame
3031
from pandas.types.cast import (_maybe_upcast, _infer_dtype_from_scalar,
@@ -753,7 +754,7 @@ def setitem(key, value):
753754
raise ValueError("Can only tuple-index with a MultiIndex")
754755

755756
# python 3 type errors should be raised
756-
if 'unorderable' in str(e): # pragma: no cover
757+
if _is_unorderable_exception(e):
757758
raise IndexError(key)
758759

759760
if com.is_bool_indexer(key):

pandas/tests/indexes/test_base.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .common import Base
88

99
from pandas.compat import (is_platform_windows, range, lrange, lzip, u,
10-
zip, PY3)
10+
zip, PY3, PY36)
1111
import operator
1212
import os
1313

@@ -1775,7 +1775,12 @@ def create_index(self):
17751775
def test_order(self):
17761776
idx = self.create_index()
17771777
# 9816 deprecated
1778-
if PY3:
1778+
if PY36:
1779+
with tm.assertRaisesRegexp(TypeError, "'>' not supported "
1780+
"between instances of 'str' and 'int'"):
1781+
with tm.assert_produces_warning(FutureWarning):
1782+
idx.order()
1783+
elif PY3:
17791784
with tm.assertRaisesRegexp(TypeError, "unorderable types"):
17801785
with tm.assert_produces_warning(FutureWarning):
17811786
idx.order()
@@ -1785,7 +1790,11 @@ def test_order(self):
17851790

17861791
def test_argsort(self):
17871792
idx = self.create_index()
1788-
if PY3:
1793+
if PY36:
1794+
with tm.assertRaisesRegexp(TypeError, "'>' not supported "
1795+
"between instances of 'str' and 'int'"):
1796+
result = idx.argsort()
1797+
elif PY3:
17891798
with tm.assertRaisesRegexp(TypeError, "unorderable types"):
17901799
result = idx.argsort()
17911800
else:
@@ -1795,7 +1804,11 @@ def test_argsort(self):
17951804

17961805
def test_numpy_argsort(self):
17971806
idx = self.create_index()
1798-
if PY3:
1807+
if PY36:
1808+
with tm.assertRaisesRegexp(TypeError, "'>' not supported "
1809+
"between instances of 'str' and 'int'"):
1810+
result = np.argsort(idx)
1811+
elif PY3:
17991812
with tm.assertRaisesRegexp(TypeError, "unorderable types"):
18001813
result = np.argsort(idx)
18011814
else:

pandas/tslib.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,8 @@ cdef convert_to_tsobject(object ts, object tz, object unit,
15391539
"Cannot convert Period to Timestamp "
15401540
"unambiguously. Use to_timestamp")
15411541
else:
1542-
raise TypeError('Cannot convert input to Timestamp')
1542+
raise TypeError('Cannot convert input [{}] of type {} to '
1543+
'Timestamp'.format(ts, type(ts)))
15431544

15441545
if obj.value != NPY_NAT:
15451546
_check_dts_bounds(&obj.dts)

pandas/types/common.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
""" common type operations """
22

33
import numpy as np
4-
from pandas.compat import string_types, text_type, binary_type
4+
from pandas.compat import (string_types, text_type, binary_type,
5+
PY3, PY36)
56
from pandas import lib, algos
67
from .dtypes import (CategoricalDtype, CategoricalDtypeType,
78
DatetimeTZDtype, DatetimeTZDtypeType,
@@ -188,6 +189,20 @@ def is_datetime_or_timedelta_dtype(arr_or_dtype):
188189
return issubclass(tipo, (np.datetime64, np.timedelta64))
189190

190191

192+
def _is_unorderable_exception(e):
193+
"""
194+
return a boolean if we an unorderable exception error message
195+
196+
These are different error message for PY>=3<=3.5 and PY>=3.6
197+
"""
198+
if PY36:
199+
return ("'>' not supported between instances "
200+
"of 'str' and 'int'" in str(e))
201+
elif PY3:
202+
return 'unorderable' in str(e)
203+
return False
204+
205+
191206
def is_numeric_v_string_like(a, b):
192207
"""
193208
numpy doesn't like to compare numeric arrays vs scalar string-likes

0 commit comments

Comments
 (0)