Skip to content

Commit b618eb6

Browse files
jbrockmendeljreback
authored andcommitted
CLN: Assorted cleanups (pandas-dev#27094)
1 parent 1be0561 commit b618eb6

File tree

6 files changed

+77
-58
lines changed

6 files changed

+77
-58
lines changed

pandas/core/groupby/ops.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1,
475475
else:
476476
if axis > 0:
477477
swapped = True
478-
values = values.swapaxes(0, axis)
478+
assert axis == 1, axis
479+
values = values.T
479480
if arity > 1:
480481
raise NotImplementedError("arity of more than 1 is not "
481482
"supported for the 'how' argument")

pandas/core/internals/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
from .blocks import ( # noqa: F401
23
Block, BoolBlock, CategoricalBlock, ComplexBlock, DatetimeBlock,
34
DatetimeTZBlock, ExtensionBlock, FloatBlock, IntBlock, ObjectBlock,
@@ -9,7 +10,7 @@
910
from .blocks import _safe_reshape # noqa: F401; io.packers
1011
from .blocks import make_block # noqa: F401; io.pytables, io.packers
1112
from .managers import ( # noqa: F401; reshape.concat, reshape.merge
13+
_transform_index,
1214
concatenate_block_managers)
13-
from .managers import items_overlap_with_suffix # noqa: F401; reshape.merge
1415

1516
from .blocks import _block_shape # noqa:F401; io.pytables

pandas/core/internals/blocks.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -722,16 +722,28 @@ def replace(self, to_replace, value, inplace=False, filter=None,
722722
try:
723723
values, to_replace = self._try_coerce_args(self.values,
724724
to_replace)
725-
mask = missing.mask_missing(values, to_replace)
726-
if filter is not None:
727-
filtered_out = ~self.mgr_locs.isin(filter)
728-
mask[filtered_out.nonzero()[0]] = False
725+
except (TypeError, ValueError):
726+
# GH 22083, TypeError or ValueError occurred within error handling
727+
# causes infinite loop. Cast and retry only if not objectblock.
728+
if is_object_dtype(self):
729+
raise
730+
731+
# try again with a compatible block
732+
block = self.astype(object)
733+
return block.replace(to_replace=original_to_replace,
734+
value=value,
735+
inplace=inplace,
736+
filter=filter,
737+
regex=regex,
738+
convert=convert)
739+
740+
mask = missing.mask_missing(values, to_replace)
741+
if filter is not None:
742+
filtered_out = ~self.mgr_locs.isin(filter)
743+
mask[filtered_out.nonzero()[0]] = False
729744

745+
try:
730746
blocks = self.putmask(mask, value, inplace=inplace)
731-
if convert:
732-
blocks = [b.convert(by_item=True, numeric=False,
733-
copy=not inplace) for b in blocks]
734-
return blocks
735747
except (TypeError, ValueError):
736748
# GH 22083, TypeError or ValueError occurred within error handling
737749
# causes infinite loop. Cast and retry only if not objectblock.
@@ -746,6 +758,10 @@ def replace(self, to_replace, value, inplace=False, filter=None,
746758
filter=filter,
747759
regex=regex,
748760
convert=convert)
761+
if convert:
762+
blocks = [b.convert(by_item=True, numeric=False,
763+
copy=not inplace) for b in blocks]
764+
return blocks
749765

750766
def _replace_single(self, *args, **kwargs):
751767
""" no-op on a non-ObjectBlock """

pandas/core/internals/managers.py

-42
Original file line numberDiff line numberDiff line change
@@ -1859,48 +1859,6 @@ def _compare_or_regex_search(a, b, regex=False):
18591859
return result
18601860

18611861

1862-
# TODO: this is no longer used in this module, could be moved to concat
1863-
def items_overlap_with_suffix(left, lsuffix, right, rsuffix):
1864-
"""
1865-
If two indices overlap, add suffixes to overlapping entries.
1866-
1867-
If corresponding suffix is empty, the entry is simply converted to string.
1868-
1869-
"""
1870-
to_rename = left.intersection(right)
1871-
if len(to_rename) == 0:
1872-
return left, right
1873-
else:
1874-
if not lsuffix and not rsuffix:
1875-
raise ValueError('columns overlap but no suffix specified: '
1876-
'{rename}'.format(rename=to_rename))
1877-
1878-
def renamer(x, suffix):
1879-
"""Rename the left and right indices.
1880-
1881-
If there is overlap, and suffix is not None, add
1882-
suffix, otherwise, leave it as-is.
1883-
1884-
Parameters
1885-
----------
1886-
x : original column name
1887-
suffix : str or None
1888-
1889-
Returns
1890-
-------
1891-
x : renamed column name
1892-
"""
1893-
if x in to_rename and suffix is not None:
1894-
return '{x}{suffix}'.format(x=x, suffix=suffix)
1895-
return x
1896-
1897-
lrenamer = partial(renamer, suffix=lsuffix)
1898-
rrenamer = partial(renamer, suffix=rsuffix)
1899-
1900-
return (_transform_index(left, lrenamer),
1901-
_transform_index(right, rrenamer))
1902-
1903-
19041862
def _transform_index(index, func, level=None):
19051863
"""
19061864
Apply function to all values found in index.

pandas/core/reshape/merge.py

+48-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import copy
6+
from functools import partial
67
import string
78
import warnings
89

@@ -27,8 +28,7 @@
2728
from pandas.core.arrays.categorical import _recode_for_categories
2829
import pandas.core.common as com
2930
from pandas.core.frame import _merge_doc
30-
from pandas.core.internals import (
31-
concatenate_block_managers, items_overlap_with_suffix)
31+
from pandas.core.internals import _transform_index, concatenate_block_managers
3232
import pandas.core.sorting as sorting
3333
from pandas.core.sorting import is_int64_overflow_possible
3434

@@ -555,8 +555,8 @@ def get_result(self):
555555
ldata, rdata = self.left._data, self.right._data
556556
lsuf, rsuf = self.suffixes
557557

558-
llabels, rlabels = items_overlap_with_suffix(ldata.items, lsuf,
559-
rdata.items, rsuf)
558+
llabels, rlabels = _items_overlap_with_suffix(ldata.items, lsuf,
559+
rdata.items, rsuf)
560560

561561
lindexers = {1: left_indexer} if left_indexer is not None else {}
562562
rindexers = {1: right_indexer} if right_indexer is not None else {}
@@ -1303,8 +1303,8 @@ def get_result(self):
13031303
ldata, rdata = self.left._data, self.right._data
13041304
lsuf, rsuf = self.suffixes
13051305

1306-
llabels, rlabels = items_overlap_with_suffix(ldata.items, lsuf,
1307-
rdata.items, rsuf)
1306+
llabels, rlabels = _items_overlap_with_suffix(ldata.items, lsuf,
1307+
rdata.items, rsuf)
13081308

13091309
if self.fill_method == 'ffill':
13101310
left_join_indexer = libjoin.ffill_indexer(left_indexer)
@@ -1809,3 +1809,45 @@ def validate_operand(obj):
18091809
else:
18101810
raise TypeError('Can only merge Series or DataFrame objects, '
18111811
'a {obj} was passed'.format(obj=type(obj)))
1812+
1813+
1814+
def _items_overlap_with_suffix(left, lsuffix, right, rsuffix):
1815+
"""
1816+
If two indices overlap, add suffixes to overlapping entries.
1817+
1818+
If corresponding suffix is empty, the entry is simply converted to string.
1819+
1820+
"""
1821+
to_rename = left.intersection(right)
1822+
if len(to_rename) == 0:
1823+
return left, right
1824+
1825+
if not lsuffix and not rsuffix:
1826+
raise ValueError('columns overlap but no suffix specified: '
1827+
'{rename}'.format(rename=to_rename))
1828+
1829+
def renamer(x, suffix):
1830+
"""
1831+
Rename the left and right indices.
1832+
1833+
If there is overlap, and suffix is not None, add
1834+
suffix, otherwise, leave it as-is.
1835+
1836+
Parameters
1837+
----------
1838+
x : original column name
1839+
suffix : str or None
1840+
1841+
Returns
1842+
-------
1843+
x : renamed column name
1844+
"""
1845+
if x in to_rename and suffix is not None:
1846+
return '{x}{suffix}'.format(x=x, suffix=suffix)
1847+
return x
1848+
1849+
lrenamer = partial(renamer, suffix=lsuffix)
1850+
rrenamer = partial(renamer, suffix=rsuffix)
1851+
1852+
return (_transform_index(left, lrenamer),
1853+
_transform_index(right, rrenamer))

pandas/tests/io/test_html.py

+1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ def test_bad_url_protocol(self):
262262
self.read_html('git://github.com', match='.*Water.*')
263263

264264
@network
265+
@pytest.mark.slow
265266
def test_invalid_url(self):
266267
try:
267268
with pytest.raises(URLError):

0 commit comments

Comments
 (0)