Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit67bc5fb

Browse files
fix: use pandas function to check for NaN (#750)
* fix: use pandas function to check for NaNStarting with pandas 1.0, an experimental pandas.NA value (singleton) is available to represent scalar missing values asopposed to numpy.nan. Comparing the variable with itself results in a pandas.NA value that doesn't support type-castingto boolean. Using the build-in pandas.isna function handles all pandas supported NaN values.* tests: Skip tests if pandas below required version* tests: compare expected and actual directly as lists* Fix pytest.mark.skipif spellingCo-authored-by: Peter Lamut <plamut@users.noreply.github.com>
1 parentba86b2a commit67bc5fb

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

‎google/cloud/bigquery/_pandas_helpers.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ def dataframe_to_json_generator(dataframe):
780780
output= {}
781781
forcolumn,valueinzip(dataframe.columns,row):
782782
# Omit NaN values.
783-
ifvalue!=value:
783+
ifpandas.isna(value):
784784
continue
785785
output[column]=value
786786
yieldoutput

‎tests/unit/test__pandas_helpers.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
importoperator
2020
importqueue
2121
importwarnings
22+
importpkg_resources
2223

2324
importmock
2425

@@ -47,6 +48,14 @@
4748
exceptImportError:# pragma: NO COVER
4849
bigquery_storage=None
4950

51+
PANDAS_MINIUM_VERSION=pkg_resources.parse_version("1.0.0")
52+
53+
ifpandasisnotNone:
54+
PANDAS_INSTALLED_VERSION=pkg_resources.get_distribution("pandas").parsed_version
55+
else:
56+
# Set to less than MIN version.
57+
PANDAS_INSTALLED_VERSION=pkg_resources.parse_version("0.0.0")
58+
5059

5160
skip_if_no_bignumeric=pytest.mark.skipif(
5261
not_BIGNUMERIC_SUPPORT,reason="BIGNUMERIC support requires pyarrow>=3.0.0",
@@ -734,6 +743,37 @@ def test_list_columns_and_indexes_with_named_index_same_as_column_name(
734743
assertcolumns_and_indexes==expected
735744

736745

746+
@pytest.mark.skipif(
747+
pandasisNoneorPANDAS_INSTALLED_VERSION<PANDAS_MINIUM_VERSION,
748+
reason="Requires `pandas version >= 1.0.0` which introduces pandas.NA",
749+
)
750+
deftest_dataframe_to_json_generator(module_under_test):
751+
utcnow=datetime.datetime.utcnow()
752+
df_data=collections.OrderedDict(
753+
[
754+
("a_series", [pandas.NA,2,3,4]),
755+
("b_series", [0.1,float("NaN"),0.3,0.4]),
756+
("c_series", ["a","b",pandas.NA,"d"]),
757+
("d_series", [utcnow,utcnow,utcnow,pandas.NaT]),
758+
("e_series", [True,False,True,None]),
759+
]
760+
)
761+
dataframe=pandas.DataFrame(
762+
df_data,index=pandas.Index([4,5,6,7],name="a_index")
763+
)
764+
765+
dataframe=dataframe.astype({"a_series":pandas.Int64Dtype()})
766+
767+
rows=module_under_test.dataframe_to_json_generator(dataframe)
768+
expected= [
769+
{"b_series":0.1,"c_series":"a","d_series":utcnow,"e_series":True},
770+
{"a_series":2,"c_series":"b","d_series":utcnow,"e_series":False},
771+
{"a_series":3,"b_series":0.3,"d_series":utcnow,"e_series":True},
772+
{"a_series":4,"b_series":0.4,"c_series":"d"},
773+
]
774+
assertlist(rows)==expected
775+
776+
737777
@pytest.mark.skipif(pandasisNone,reason="Requires `pandas`")
738778
deftest_list_columns_and_indexes_with_named_index(module_under_test):
739779
df_data=collections.OrderedDict(

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp