I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
import numpy as np import pandas as pd index = pd.MultiIndex.from_tuples([(0, "a"), (0, "b")]) x = pd.Series([1, 2], index=index) print(x.to_numpy(dtype="float")) # [1. 2.] print(x.to_numpy(dtype="float", na_value=np.nan)) # KeyError: 1Issue Description
When converting a multiindexed series to a numpy array, it works correctly when no na_value
is specified. But when an na_value
is specified, even if the series has no NA values, it errors.
Digging into this issue, when na_value
is specified, a branch is taken which sets the na_value
in the numpy array.result[self.isna()] = na_value
However, the numpy result
array cannot understand the multiindex of the self.isna()
multi-indexed array.
The Pull Request #45775 is one possibility to address this issue.
Expected Behaviorimport numpy as np import pandas as pd index = pd.MultiIndex.from_tuples([(0, "a"), (0, "b")]) x = pd.Series([1, 2], index=index) print(x.to_numpy(dtype="float")) # [1. 2.] print(x.to_numpy(dtype="float", na_value=np.nan)) # [1. 2.]Installed Versions
The pd.show_versions()
produces an error, but below are the versions of the relevant packages.
pandas 1.5.0.dev0+268.gbe8d1ec880 dev_0 # Installed locally
numpy 1.22.0 pypi_0 pypi
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4