-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Comments
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](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
—
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: ***@***.***>
|
Thank you for your encouragement. But I also found two other problems in file My environment is:python 3.8, pandas 2.0.3, numpy 1.24.3 Question 1source code
errorWhen running this part of the code above, the system prompts the following error warning:
solutionI haven't found a solution for this part yet Question 2source code
errorWhen running this part of the code above, the system prompts the following error warning:
solutionAfter I changed
|
When I run the
tools_pandas.ipynb file
, I get an error in the code at "To specify missing values, you can either usenp.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
The error is
method settle an issue
I was able to avoid this error by replacing
np.object
withobject
after looking into it.The text was updated successfully, but these errors were encountered: