-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
ENH: Add na_value argument to DataFrame.to_numpy #33857
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
Changes from 1 commit
054d74e
cafbf5f
34b9b9f
9a2bbd6
f7dc246
5eb8bb2
ec2f729
b48b6c9
d1a60e8
09fdf51
f5db15a
89e8930
d24b976
bec3889
a20f116
02405a1
055413f
ae088e4
d78ba29
9c87e00
df5b683
ae2b34a
bcb69c5
c3a7a55
b5ec43f
f3e45d7
e54cc28
491a5ae
4ecccff
142c808
8d42fd4
c2228bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1284,7 +1284,9 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra | |
|
||
return cls(data, index=index, columns=columns, dtype=dtype) | ||
|
||
def to_numpy(self, dtype=None, copy: bool = False) -> np.ndarray: | ||
def to_numpy( | ||
self, dtype=None, copy: bool = False, na_value=lib.no_default | ||
) -> np.ndarray: | ||
""" | ||
Convert the DataFrame to a NumPy array. | ||
|
||
|
@@ -1305,6 +1307,11 @@ def to_numpy(self, dtype=None, copy: bool = False) -> np.ndarray: | |
another array. Note that ``copy=False`` does not *ensure* that | ||
``to_numpy()`` is no-copy. Rather, ``copy=True`` ensure that | ||
a copy is made, even if not strictly necessary. | ||
na_value : Any, optional | ||
The value to use for missing values. The default value depends | ||
on `dtype` and the type of the array. | ||
dsaxton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. versionadded:: 1.1.0 | ||
|
||
Returns | ||
------- | ||
|
@@ -1337,6 +1344,10 @@ def to_numpy(self, dtype=None, copy: bool = False) -> np.ndarray: | |
[2, 4.5, Timestamp('2000-01-02 00:00:00')]], dtype=object) | ||
""" | ||
result = np.array(self.values, dtype=dtype, copy=copy) | ||
dsaxton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if na_value is not lib.no_default: | ||
result[isna(result)] = na_value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
do we want a more user friendly message, or perhaps validate the fill_value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That will be difficult in general, I think (since it depends on the behaviour of numpy on what it accepts as value to set to the array). Now, for an extension array, you already get a slightly better error message:
|
||
|
||
return result | ||
|
||
def to_dict(self, orient="dict", into=dict): | ||
|
Uh oh!
There was an error while loading. Please reload this page.