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

[BUG] tools_pandas.ipynb - module 'numpy' has no attribute 'object' #633

Open
yffdd opened this issue Mar 26, 2024 · 2 comments
Open

[BUG] tools_pandas.ipynb - module 'numpy' has no attribute 'object' #633

yffdd opened this issue Mar 26, 2024 · 2 comments

Comments

@yffdd
Copy link

yffdd commented Mar 26, 2024

When I run the tools_pandas.ipynb file, I get an error in the code at "To specify missing values, you can either use np.nan or NumPy's masked arrays" under DataFrame objects.

My environment is:python 3.8, pandas 2.0.3, numpy 1.24.3,

The source code is

masked_array = np.ma.asarray(values, dtype=np.object)
masked_array[(0, 2), (1, 2)] = np.ma.masked
d3 = pd.DataFrame(
        masked_array,
        columns=["birthyear", "children", "hobby", "weight"],
        index=["alice", "bob", "charles"]
     )
d3

The error is

AttributeError                            Traceback (most recent call last)
Cell In[84], [line 1](vscode-notebook-cell:?execution_count=84&line=1)
----> [1](vscode-notebook-cell:?execution_count=84&line=1) masked_array = np.ma.asarray(values, dtype=np.object)
      [2](vscode-notebook-cell:?execution_count=84&line=2) masked_array[(0, 2), (1, 2)] = np.ma.masked
      [3](vscode-notebook-cell:?execution_count=84&line=3) d3 = pd.DataFrame(
      [4](vscode-notebook-cell:?execution_count=84&line=4)         masked_array,
      [5](vscode-notebook-cell:?execution_count=84&line=5)         columns=["birthyear", "children", "hobby", "weight"],
      [6](vscode-notebook-cell:?execution_count=84&line=6)         index=["alice", "bob", "charles"]
      [7](vscode-notebook-cell:?execution_count=84&line=7)      )

File [f:\ToolSoftware\anaconda3\envs\benv_py3.8\lib\site-packages\numpy\__init__.py:305](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/numpy/__init__.py:305), in __getattr__(attr)
    [300](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/numpy/__init__.py:300)     warnings.warn(
    [301](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/numpy/__init__.py:301)         f"In the future `np.{attr}` will be defined as the "
    [302](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/numpy/__init__.py:302)         "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
    [304](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/numpy/__init__.py:304) if attr in __former_attrs__:
--> [305](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/numpy/__init__.py:305)     raise AttributeError(__former_attrs__[attr])
    [307](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/numpy/__init__.py:307) # Importing Tester requires importing all of UnitTest which is not a
    [308](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/numpy/__init__.py:308) # cheap import Since it is mainly used in test suits, we lazy import it
    [309](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/numpy/__init__.py:309) # here to save on the order of 10 ms of import time for most users
    [310](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/numpy/__init__.py:310) #
    [311](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/numpy/__init__.py:311) # The previous way Tester was imported also had a side effect of adding
    [312](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/numpy/__init__.py:312) # the full `numpy.testing` namespace
    [313](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/numpy/__init__.py:313) if attr == 'testing':

AttributeError: module 'numpy' has no attribute 'object'.

method settle an issue

I was able to avoid this error by replacing np.object with object after looking into it.

masked_array = np.ma.asarray(values, dtype=object)
masked_array[(0, 2), (1, 2)] = np.ma.masked
d3 = pd.DataFrame(
        masked_array,
        columns=["birthyear", "children", "hobby", "weight"],
        index=["alice", "bob", "charles"]
     )
d3
@leonardopacher
Copy link

leonardopacher commented Mar 26, 2024 via email

@yffdd
Copy link
Author

yffdd commented Mar 27, 2024

Thank you for your encouragement. But I also found two other problems in file tools_pandas.ipynb today.

My environment is:python 3.8, pandas 2.0.3, numpy 1.24.3

Question 1

source code

city_loc.append(city_pop)

error

When running this part of the code above, the system prompts the following error warning:

AttributeError                            Traceback (most recent call last)
[~\AppData\Local\Temp\ipykernel_3764\360768564.py](https://file+.vscode-resource.vscode-cdn.net/d%3A/jupyter/machine_learning_handson/handson-ml2-master/~/AppData/Local/Temp/ipykernel_3764/360768564.py) in ?()
----> 1 city_loc.append(city_pop)

[f:\ToolSoftware\anaconda3\envs\benv_py3.8\lib\site-packages\pandas\core\generic.py](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/generic.py) in ?(self, name)
   5985             and name not in self._accessors
   5986             and self._info_axis._can_hold_identifiers_and_holds_name(name)
   5987         ):
   5988             return self[name]
-> 5989         return object.__getattribute__(self, name)

AttributeError: 'DataFrame' object has no attribute 'append'

solution

I haven't found a solution for this part yet

Question 2

source code

city_eco["economy"].cat.categories = ["Finance", "Energy", "Tourism"]
city_eco

error

When running this part of the code above, the system prompts the following error warning:

AttributeError                            Traceback (most recent call last)
Cell In[155], [line 1](vscode-notebook-cell:?execution_count=155&line=1)
----> [1](vscode-notebook-cell:?execution_count=155&line=1) city_eco["economy"].cat.categories = ["Finance", "Energy", "Tourism"]
      [2](vscode-notebook-cell:?execution_count=155&line=2) # city_eco["economy"].cat.rename_categories = ["Finance", "Energy", "Tourism"]
      [4](vscode-notebook-cell:?execution_count=155&line=4) city_eco

File [f:\ToolSoftware\anaconda3\envs\benv_py3.8\lib\site-packages\pandas\core\base.py:178](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/base.py:178), in NoNewAttributesMixin.__setattr__(self, key, value)
    [172](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/base.py:172) if getattr(self, "__frozen", False) and not (
    [173](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/base.py:173)     key == "_cache"
    [174](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/base.py:174)     or key in type(self).__dict__
    [175](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/base.py:175)     or getattr(self, key, None) is not None
    [176](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/base.py:176) ):
    [177](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/base.py:177)     raise AttributeError(f"You cannot add any new attribute '{key}'")
--> [178](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/base.py:178) object.__setattr__(self, key, value)

File [f:\ToolSoftware\anaconda3\envs\benv_py3.8\lib\site-packages\pandas\core\accessor.py:99](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/accessor.py:99), in PandasDelegate._add_delegate_accessors.<locals>._create_delegator_property.<locals>._setter(self, new_values)
     [98](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/accessor.py:98) def _setter(self, new_values):
---> [99](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/accessor.py:99)     return self._delegate_property_set(name, new_values)

File [f:\ToolSoftware\anaconda3\envs\benv_py3.8\lib\site-packages\pandas\core\arrays\categorical.py:2460](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/arrays/categorical.py:2460), in CategoricalAccessor._delegate_property_set(self, name, new_values)
   [2459](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/arrays/categorical.py:2459) def _delegate_property_set(self, name, new_values):
-> [2460](file:///F:/ToolSoftware/anaconda3/envs/benv_py3.8/lib/site-packages/pandas/core/arrays/categorical.py:2460)     return setattr(self._parent, name, new_values)

AttributeError: can't set attribute

solution

After I changed categories to rename_categories , the program works fine.
The changed code is as follows:

city_eco["economy"].cat.rename_categories = ["Finance", "Energy", "Tourism"]
city_eco

It appears you've run into a common pitfall with newer versions of NumPy and pandas. Indeed, you've stumbled upon the correct solution to your problem. In recent versions of NumPy, np.object has been deprecated in favor of simply using object. This change reflects a broader push to align with Python's built-in types and reduce confusion. Here's what happened: 1. Your original code used np.object, which, as of your NumPy version, raises an AttributeError because it's no longer a valid attribute. 2. By replacing np.object with object, you're telling NumPy to use Python's built-in object type for the array's data type, which is perfectly acceptable and the recommended approach moving forward. So, your solution is spot-on. When you updated your code to: python masked_array = np.ma.asarray(values, dtype=object) masked_array[(0, 2), (1, 2)] = np.ma.masked d3 = pd.DataFrame( masked_array, columns=["birthyear", "children", "hobby", "weight"], index=["alice", "bob", "charles"] ) You effectively adapted to the newer standards, ensuring compatibility and sidestepping the AttributeError. Keep this adaptation in mind for future code, as the deprecation of older terms like np.object in favor of more standard Python types is part of the ongoing evolution of the NumPy library. Well done on debugging this!

On Tue, 26 Mar 2024 at 09:57, xiales @.> wrote: When I run the tools_pandas.ipynb file, I get an error in the code at "To specify missing values, you can either use np.nan or NumPy's masked arrays" under DataFrame objects. My environment is:python 3.8, pandas 2.0.3, numpy 1.24.3, The source code is masked_array = np.ma.asarray(values, dtype=np.object) masked_array[(0, 2), (1, 2)] = np.ma.masked d3 = pd.DataFrame( masked_array, columns=["birthyear", "children", "hobby", "weight"], index=["alice", "bob", "charles"] ) d3 The error is AttributeError Traceback (most recent call last) Cell In[84], line 1 ----> 1 masked_array = np.ma.asarray(values, dtype=np.object) 2 masked_array[(0, 2), (1, 2)] = np.ma.masked 3 d3 = pd.DataFrame( 4 masked_array, 5 columns=["birthyear", "children", "hobby", "weight"], 6 index=["alice", "bob", "charles"] 7 ) File f:\ToolSoftware\anaconda3\envs\benv_py3.8\lib\site-packages\numpy_init_.py:305, in getattr(attr) 300 warnings.warn( 301 f"In the future np.{attr} will be defined as the " 302 "corresponding NumPy scalar.", FutureWarning, stacklevel=2) 304 if attr in former_attrs: --> 305 raise AttributeError(former_attrs[attr]) 307 # Importing Tester requires importing all of UnitTest which is not a 308 # cheap import Since it is mainly used in test suits, we lazy import it 309 # here to save on the order of 10 ms of import time for most users 310 # 311 # The previous way Tester was imported also had a side effect of adding 312 # the full numpy.testing namespace 313 if attr == 'testing': AttributeError: module 'numpy' has no attribute 'object'. method settle an issue I was able to avoid this error by replacing np.object with object after looking into it. masked_array = np.ma.asarray(values, dtype=object) masked_array[(0, 2), (1, 2)] = np.ma.masked d3 = pd.DataFrame( masked_array, columns=["birthyear", "children", "hobby", "weight"], index=["alice", "bob", "charles"] ) d3 — Reply to this email directly, view it on GitHub <#633>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEU3ISQZXXTS7YTXAD7NTQLY2FWB7AVCNFSM6AAAAABFI4AQSWVHI2DSMVQWIX3LMV43ASLTON2WKOZSGIYDQMJXGEZDQOI . You are receiving this because you are subscribed to this thread.Message ID: @.>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants