Skip to content
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

.replace() raises when replacing strings #21078

Closed
carlosdanielcsantos opened this issue May 16, 2018 · 9 comments · Fixed by #21098
Closed

.replace() raises when replacing strings #21078

carlosdanielcsantos opened this issue May 16, 2018 · 9 comments · Fixed by #21098
Labels
Compat pandas objects compatability with Numpy or Python functions
Milestone

Comments

@carlosdanielcsantos
Copy link
Contributor

carlosdanielcsantos commented May 16, 2018

Code Sample, a copy-pastable example if possible

pd.Series(['a', 'b', 'c']).replace('a', 'x')

Problem description

This raises the following error:

TypeError: Type aliases cannot be used with isinstance().

This behavior is exclusive to the new version 0.23. Previous versions work as expected.

Full traceback

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-75-761515b509b4> in <module>()
----> 1 pd.Series(['a','b','c']).replace('a', 'x')

~/.virtualenvs/new/lib/python3.5/site-packages/pandas/core/series.py in replace(self, to_replace, value, inplace, limit, regex, method)
   3427         return super(Series, self).replace(to_replace=to_replace, value=value,
   3428                                            inplace=inplace, limit=limit,
-> 3429                                            regex=regex, method=method)
   3430
   3431     @Appender(generic._shared_docs['shift'] % _shared_doc_kwargs)

~/.virtualenvs/new/lib/python3.5/site-packages/pandas/core/generic.py in replace(self, to_replace, value, inplace, limit, regex, method)
   5877                     new_data = self._data.replace(to_replace=to_replace,
   5878                                                   value=value, inplace=inplace,
-> 5879                                                   regex=regex)
   5880                 else:
   5881                     msg = ('Invalid "to_replace" type: '

~/.virtualenvs/new/lib/python3.5/site-packages/pandas/core/internals.py in replace(self, **kwargs)
   3718
   3719     def replace(self, **kwargs):
-> 3720         return self.apply('replace', **kwargs)
   3721
   3722     def replace_list(self, src_list, dest_list, inplace=False, regex=False,

~/.virtualenvs/new/lib/python3.5/site-packages/pandas/core/internals.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
   3579
   3580             kwargs['mgr'] = self
-> 3581             applied = getattr(b, f)(**kwargs)
   3582             result_blocks = _extend_blocks(applied, result_blocks)
   3583

~/.virtualenvs/new/lib/python3.5/site-packages/pandas/core/internals.py in replace(self, to_replace, value, inplace, filter, regex, convert, mgr)
   2430         blocks = [self]
   2431
-> 2432         if not either_list and is_re(to_replace):
   2433             return self._replace_single(to_replace, value, inplace=inplace,
   2434                                         filter=filter, regex=True,

~/.virtualenvs/new/lib/python3.5/site-packages/pandas/core/dtypes/inference.py in is_re(obj)
    217     """
    218
--> 219     return isinstance(obj, re_type)
    220
    221

/usr/lib/python3.5/typing.py in __instancecheck__(self, obj)
    256
    257     def __instancecheck__(self, obj):
--> 258         raise TypeError("Type aliases cannot be used with isinstance().")
    259
    260     def __subclasscheck__(self, cls):

TypeError: Type aliases cannot be used with isinstance().

Expected Output

0    x
1    b
2    c
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.13.13-5-pve
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.0
pytest: None
pip: 8.1.1
setuptools: 20.7.0
Cython: None
numpy: 1.14.3
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@TomAugspurger
Copy link
Contributor

Hmm I'm not able to reproduce on Python 3.6.

Can you past the full traceback?

@TomAugspurger
Copy link
Contributor

I don't understand the bit about "type aliases". Are you overriding a builting with something from the typing module?

@carlosdanielcsantos
Copy link
Contributor Author

@TomAugspurger I added the full traceback

@TomAugspurger
Copy link
Contributor

Does this raise for you?

In [10]: import re

In [11]: import typing

In [12]: isinstance(re.compile(''), typing.re.Pattern)
Out[12]: True

If so, can you check the Python issue tracker to see if this has been filed?

@TomAugspurger
Copy link
Contributor

That works fine on 3.5.4 for me.

Python 3.5.4 | packaged by conda-forge | (default, Aug 10 2017, 01:41:15)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> import typing
>>> isinstance(re.compile(''), typing.re.Pattern)
True
>>>

Can you try upgrading from 3.5.1 to a later point release?

@carlosdanielcsantos
Copy link
Contributor Author

It definitely works with 3.5.4 but doesn't work with 3.5.2 either

@TomAugspurger
Copy link
Contributor

OK. See #20553 for reference.

If you want, you can make a PR using the old way for Python 3.5.2 (or 3) and older... Or upgrade to the latest point release

@carlosdanielcsantos
Copy link
Contributor Author

Will do, thanks!

@adamabk
Copy link
Contributor

adamabk commented May 17, 2018

I had the same issue using the same Python 3.5.2. I just opened up the PR for it to revert it back.
Was that what you mean @TomAugspurger?

@jreback jreback added this to the 0.23.1 milestone May 17, 2018
@jreback jreback added the Compat pandas objects compatability with Numpy or Python functions label May 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants