Skip to content

COMPAT: ensure platform int on 32-bit #13047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,8 @@ def dot(self, other):
@Substitution(klass='Series', value='v')
@Appender(base._shared_docs['searchsorted'])
def searchsorted(self, v, side='left', sorter=None):
if sorter is not None:
sorter = com._ensure_platform_int(sorter)
return self._values.searchsorted(Series(v)._values,
side=side, sorter=sorter)

Expand Down
5 changes: 4 additions & 1 deletion pandas/indexes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ def conv(i):
if not index.equals(other):
return _unique_indices(indexes)

return index
name = _get_consensus_names(indexes)[0]
if name != index.name:
index = index._shallow_copy(name=name)
return index
else:
return _unique_indices(indexes)

Expand Down
21 changes: 18 additions & 3 deletions pandas/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,21 @@ def __or__(self, other):
def __xor__(self, other):
return self.symmetric_difference(other)

def _get_consensus_name(self, other):
"""
Given 2 indexes, give a consensus name meaning
we take the not None one, or None if the names differ.
Return a new object if we are resetting the name
"""
if self.name != other.name:
if self.name is None or other.name is None:
name = self.name or other.name
else:
name = None
if self.name != name:
return other._shallow_copy(name=name)
return self

def union(self, other):
"""
Form the union of two Index objects and sorts if possible.
Expand All @@ -1688,10 +1703,10 @@ def union(self, other):
other = _ensure_index(other)

if len(other) == 0 or self.equals(other):
return self
return self._get_consensus_name(other)

if len(self) == 0:
return other
return other._get_consensus_name(self)

if not com.is_dtype_equal(self.dtype, other.dtype):
this = self.astype('O')
Expand Down Expand Up @@ -1774,7 +1789,7 @@ def intersection(self, other):
other = _ensure_index(other)

if self.equals(other):
return self
return self._get_consensus_name(other)

if not com.is_dtype_equal(self.dtype, other.dtype):
this = self.astype('O')
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import re

from pandas.compat import (range, zip, lrange, StringIO, PY3,
u, lzip, is_platform_windows)
u, lzip, is_platform_windows,
is_platform_32bit)
import pandas.compat as compat
import itertools
from operator import methodcaller
Expand Down Expand Up @@ -45,6 +46,8 @@

import nose

use_32bit_repr = is_platform_windows() or is_platform_32bit()

_frame = DataFrame(tm.getSeriesData())


Expand Down Expand Up @@ -3758,7 +3761,7 @@ def test_to_string_header(self):
def test_sparse_max_row(self):
s = pd.Series([1, np.nan, np.nan, 3, np.nan]).to_sparse()
result = repr(s)
dtype = '' if is_platform_windows() else ', dtype=int32'
dtype = '' if use_32bit_repr else ', dtype=int32'
exp = ("0 1.0\n1 NaN\n2 NaN\n3 3.0\n"
"4 NaN\ndtype: float64\nBlockIndex\n"
"Block locations: array([0, 3]{0})\n"
Expand Down
61 changes: 57 additions & 4 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,23 @@ def test_intersection(self):
result = idx1.intersection(idx2)
self.assertTrue(result.equals(expected))

# preserve names
first = self.strIndex[5:20]
second = self.strIndex[:10]
first.name = 'A'
second.name = 'A'
intersect = first.intersection(second)
self.assertEqual(intersect.name, 'A')

second.name = 'B'
intersect = first.intersection(second)
self.assertIsNone(intersect.name)

first.name = None
second.name = 'B'
intersect = first.intersection(second)
self.assertIsNone(intersect.name)

def test_union(self):
first = self.strIndex[5:20]
second = self.strIndex[:10]
Expand All @@ -578,15 +595,51 @@ def test_union(self):
self.assertIs(union, first)

# preserve names
first.name = 'A'
second.name = 'A'
first = Index(list('ab'), name='A')
second = Index(list('ab'), name='B')
union = first.union(second)
self.assertEqual(union.name, 'A')
self.assertIsNone(union.name)

second.name = 'B'
first = Index(list('ab'), name='A')
second = Index([], name='B')
union = first.union(second)
self.assertIsNone(union.name)

first = Index([], name='A')
second = Index(list('ab'), name='B')
union = first.union(second)
self.assertIsNone(union.name)

first = Index(list('ab'))
second = Index(list('ab'), name='B')
union = first.union(second)
self.assertEqual(union.name, 'B')

first = Index([])
second = Index(list('ab'), name='B')
union = first.union(second)
self.assertEqual(union.name, 'B')

first = Index(list('ab'))
second = Index([], name='B')
union = first.union(second)
self.assertEqual(union.name, 'B')

first = Index(list('ab'), name='A')
second = Index(list('ab'))
union = first.union(second)
self.assertEqual(union.name, 'A')

first = Index(list('ab'), name='A')
second = Index([])
union = first.union(second)
self.assertEqual(union.name, 'A')

first = Index([], name='A')
second = Index(list('ab'))
union = first.union(second)
self.assertEqual(union.name, 'A')

def test_add(self):

# - API change GH 8226
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexing/test_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,11 @@ def _assert_replace_conversion(self, from_key, to_key, how):
# TODO_GH12747 The result must be int?
(from_key == 'bool' and to_key in ('int64'))):

# buggy on 32-bit
if tm.is_platform_32bit():
raise nose.SkipTest("32-bit platform buggy: {0} -> {1}".format
(from_key, to_key))

# Expected: do not downcast by replacement
exp = pd.Series(self.rep[to_key], index=index,
name='yyy', dtype=from_key)
Expand All @@ -481,6 +486,7 @@ def _assert_replace_conversion(self, from_key, to_key, how):
exp = pd.Series(self.rep[to_key], index=index, name='yyy')
self.assertEqual(exp.dtype, to_key)


tm.assert_series_equal(result, exp)

def test_replace_conversion_dict_from_object(self):
Expand Down