Skip to content

Commit 0629cac

Browse files
authored
fix: fix the default value for na_value for numpy conversions (#1766)
1 parent 8e71b03 commit 0629cac

File tree

7 files changed

+34
-7
lines changed

7 files changed

+34
-7
lines changed

bigframes/dataframe.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import google.cloud.bigquery as bigquery
4848
import numpy
4949
import pandas
50+
from pandas.api import extensions as pd_ext
5051
import pandas.io.formats.format
5152
import pyarrow
5253
import tabulate
@@ -4097,7 +4098,7 @@ def to_numpy(
40974098
self,
40984099
dtype=None,
40994100
copy=False,
4100-
na_value=None,
4101+
na_value=pd_ext.no_default,
41014102
*,
41024103
allow_large_results=None,
41034104
**kwargs,

bigframes/series.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import google.cloud.bigquery as bigquery
4343
import numpy
4444
import pandas
45+
from pandas.api import extensions as pd_ext
4546
import pandas.core.dtypes.common
4647
import pyarrow as pa
4748
import typing_extensions
@@ -2109,7 +2110,7 @@ def to_numpy(
21092110
self,
21102111
dtype=None,
21112112
copy=False,
2112-
na_value=None,
2113+
na_value=pd_ext.no_default,
21132114
*,
21142115
allow_large_results=None,
21152116
**kwargs,

tests/system/small/test_dataframe_io.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from typing import Tuple
1616

1717
import google.api_core.exceptions
18+
import numpy
19+
import numpy.testing
1820
import pandas as pd
1921
import pandas.testing
2022
import pyarrow as pa
@@ -1061,3 +1063,12 @@ def test_to_sql_query_named_index_excluded(
10611063
utils.assert_pandas_df_equal(
10621064
roundtrip.to_pandas(), pd_df, check_index_type=False, ignore_order=True
10631065
)
1066+
1067+
1068+
def test_to_numpy(scalars_dfs):
1069+
bf_df, pd_df = scalars_dfs
1070+
1071+
bf_result = numpy.array(bf_df[["int64_too"]], dtype="int64")
1072+
pd_result = numpy.array(pd_df[["int64_too"]], dtype="int64")
1073+
1074+
numpy.testing.assert_array_equal(bf_result, pd_result)

tests/system/small/test_series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2132,7 +2132,7 @@ def test_drop_duplicates(scalars_df_index, scalars_pandas_df_index, keep, col_na
21322132
],
21332133
)
21342134
def test_unique(scalars_df_index, scalars_pandas_df_index, col_name):
2135-
bf_uniq = scalars_df_index[col_name].unique().to_numpy()
2135+
bf_uniq = scalars_df_index[col_name].unique().to_numpy(na_value=None)
21362136
pd_uniq = scalars_pandas_df_index[col_name].unique()
21372137
numpy.array_equal(pd_uniq, bf_uniq)
21382138

tests/system/small/test_series_io.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import numpy
15+
import numpy.testing
1416
import pandas as pd
1517
import pytest
1618

@@ -114,3 +116,12 @@ def test_to_pandas_batches(scalars_dfs, page_size, max_results, allow_large_resu
114116
total_rows += actual_rows
115117

116118
assert total_rows == expected_total_rows
119+
120+
121+
def test_to_numpy(scalars_dfs):
122+
bf_df, pd_df = scalars_dfs
123+
124+
bf_result = numpy.array(bf_df["int64_too"], dtype="int64")
125+
pd_result = numpy.array(pd_df["int64_too"], dtype="int64")
126+
127+
numpy.testing.assert_array_equal(bf_result, pd_result)

third_party/bigframes_vendored/pandas/core/frame.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import bigframes_vendored.pandas.core.generic as generic
1818
import numpy as np
1919
import pandas as pd
20+
from pandas.api import extensions as pd_ext
2021

2122
# -----------------------------------------------------------------------
2223
# DataFrame class
@@ -369,7 +370,7 @@ def to_numpy(
369370
self,
370371
dtype=None,
371372
copy=False,
372-
na_value=None,
373+
na_value=pd_ext.no_default,
373374
*,
374375
allow_large_results=None,
375376
**kwargs,

third_party/bigframes_vendored/pandas/core/series.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
from bigframes_vendored.pandas.core.generic import NDFrame
2020
import numpy
2121
import numpy as np
22-
from pandas._libs import lib
2322
from pandas._typing import Axis, FilePath, NaPosition, WriteBuffer
23+
from pandas.api import extensions as pd_ext
2424

2525
from bigframes import constants
2626

@@ -323,7 +323,7 @@ def reset_index(
323323
self,
324324
*,
325325
drop: bool = False,
326-
name=lib.no_default,
326+
name=pd_ext.no_default,
327327
) -> DataFrame | Series | None:
328328
"""
329329
Generate a new DataFrame or Series with the index reset.
@@ -730,7 +730,9 @@ def tolist(self, *, allow_large_results: Optional[bool] = None) -> list:
730730

731731
to_list = tolist
732732

733-
def to_numpy(self, dtype, copy=False, na_value=None, *, allow_large_results=None):
733+
def to_numpy(
734+
self, dtype, copy=False, na_value=pd_ext.no_default, *, allow_large_results=None
735+
):
734736
"""
735737
A NumPy ndarray representing the values in this Series or Index.
736738

0 commit comments

Comments
 (0)