Reputation: 1
I'm trying to learn pandas to perform a job that includes a lot of wrangling with text data so I've been exploring the methods available for working with text data. I first encountered this problem by trying to use the .str.replace() on a series, for example I tried using it on a Series constructed using the pd.Series() method, and then I tried extracting a column from a dataframe and using the method on that series. Both times I receive an error that the method can't be called on a 'PandasArray' object. I further tested some other string methods such as .str.contains(), .str.startswith(), .str.title() but receive the same error. I closed jupyter notebook and restarted my computer before retesting the code to check I wasn't using the names somewhere else. I tried reinstalling pandas using
conda install pandas --force--reinstall
but I continue to receive the error. I checked the version which is 1.1.5 using
pd.__version__
I am not sure why it's interpreting my series as a 'pandasarray'. The string methods are important for me, but I'm unsure how to solve this. I tried these methods on my personal computer at home using the same code and it worked properly, I thought that reinstalling pandas would fix the issue. Here's the code I tested with multiple times:
import pandas as pd
import numpy as np
testseries = pd.Series(['Foo','Foh',np.nan])
testseries.str.replace(r'Fo','Ba')
testdataframe = pd.DataFrame({'a':['Foo','Foh',np.nan],'b':['Foo','Foh',np.nan]})
testdataframe['a'].str.replace('Fo','Ba')
Error message:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-0067b28206a2> in <module>
----> 1 testdataframe['a'].str.replace('Fo','Ba')
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\strings\accessor.py in wrapper(self, *args, **kwargs)
99 )
100 raise TypeError(msg)
--> 101 return func(self, *args, **kwargs)
102
103 wrapper.__name__ = func_name
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\strings\accessor.py in replace(self, pat, repl, n, case, flags, regex)
1310 warnings.warn(msg, FutureWarning, stacklevel=3)
1311 regex = True
-> 1312 result = self._array._str_replace(
1313 pat, repl, n=n, case=case, flags=flags, regex=regex
1314 )
AttributeError: 'PandasArray' object has no attribute '_str_replace'
Upvotes: 0
Views: 3731
Reputation: 3313
Faced the same issue using pandas 1.1.3 (in a conda environment). Got it solved by updating to 1.2.0
Upvotes: 1
Reputation: 338
In my enviroment python 3.8
and pandas version 1.1.3
the code works just fine.In any case you can experiment with numpy
,where you won't have version issues, for string operations and see if it meets your requirements.
Upvotes: 0