Skip to content

Commit d2d321d

Browse files
authored
Fix failing tests by adding a workaround for pandas 3.0 dev (#4009)
1 parent 6ad90eb commit d2d321d

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

pygmt/tests/test_datatypes_dataset.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pandas as pd
88
import pytest
9+
from packaging.version import Version
910
from pygmt import which
1011
from pygmt.clib import Session
1112
from pygmt.helpers import GMTTempFile
@@ -25,8 +26,12 @@ def dataframe_from_pandas(filepath_or_buffer, sep=r"\s+", comment="#", header=No
2526

2627
# By default, pandas reads text strings with whitespaces as multiple columns, but
2728
# GMT concatenates all trailing text as a single string column. Need do find all
28-
# string columns (with dtype="object") and combine them into a single string column.
29-
string_columns = df.select_dtypes(include=["object"]).columns
29+
# string columns and combine them into a single string column.
30+
# For pandas<3.0, string columns have dtype="object".
31+
# For pandas>=3.0, string columns have dtype="str".
32+
# TODO(pandas>=3.0): Remove the pandas version check.
33+
dtype = "object" if Version(pd.__version__) < Version("3.0.0.dev0") else "str"
34+
string_columns = df.select_dtypes(include=[dtype]).columns
3035
if len(string_columns) > 1:
3136
df[string_columns[0]] = df[string_columns].apply(lambda x: " ".join(x), axis=1)
3237
df = df.drop(string_columns[1:], axis=1)

0 commit comments

Comments
 (0)