GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve the workarounds for handling pandas nullable dtypes in pandas<=2.1 by seisman · Pull Request #3596 · GenericMappingTools/pygmt

Improve the workarounds for handling pandas nullable dtypes in pandas<=2.1 #3596

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.

Sign up for GitHub

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

Merged
seisman merged 2 commits into main from pandas-2.1
Dec 12, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

Improve the workarounds for handling pandas nullable dtypes in pandas<=2.1 #3596

Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
0aa73e6
Improve the workaround for handling pandas null dtypes in pandas<=2.1
seisman Dec 2, 2024
3f7eff2
Separate variable 'dtype' and 'numpy_dtype' for the input and result …
seisman Dec 2, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

37 changes: 25 additions & 12 deletions 37 pygmt/clib/conversion.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,32 @@ def _to_numpy(data: Any) -> np.ndarray:
"date64[ms][pyarrow]": "datetime64[ms]",
}

# The dtype for the input object.
dtype = getattr(data, "dtype", getattr(data, "type", ""))
# The numpy dtype for the result numpy array, but can be None.
numpy_dtype = dtypes.get(str(dtype))

# pandas numeric dtypes were converted to np.object_ dtype prior pandas 2.2, and are
# converted to suitable NumPy dtypes since pandas 2.2. Refer to the following link
# for details: https://pandas.pydata.org/docs/whatsnew/v2.2.0.html#to-numpy-for-numpy-nullable-and-arrow-types-converts-to-suitable-numpy-dtype
#
# Workarounds for pandas < 2.2. Following SPEC 0, pandas 2.1 should be dropped in
# 2025 Q3, so it's likely we can remove the workaround in PyGMT v0.17.0.
if (
hasattr(data, "isna")
and data.isna().any()
and Version(pd.__version__) < Version("2.2")
):
# Workaround for dealing with pd.NA with pandas < 2.2.
# Bug report at: https://github.com/GenericMappingTools/pygmt/issues/2844
# Following SPEC0, pandas 2.1 will be dropped in 2025 Q3, so it's likely
# we can remove the workaround in PyGMT v0.17.0.
array = np.ascontiguousarray(data.astype(float))
else:
vec_dtype = str(getattr(data, "dtype", getattr(data, "type", "")))
array = np.ascontiguousarray(data, dtype=dtypes.get(vec_dtype))
Version(pd.__version__) < Version("2.2") # pandas < 2.2 only.
and hasattr(data, "dtype") # NumPy array or pandas objects only.
and hasattr(data.dtype, "numpy_dtype") # pandas dtypes only.
and data.dtype.kind in "iuf" # Numeric dtypes only.
): # pandas Series/Index with pandas nullable numeric dtypes.
# The numpy dtype of the result numpy array.
numpy_dtype = data.dtype.numpy_dtype
if getattr(data, "hasnans", False):
if data.dtype.kind in "iu":
# Integers with missing values are converted to float64.
numpy_dtype = np.float64
data = data.to_numpy(na_value=np.nan)

array = np.ascontiguousarray(data, dtype=numpy_dtype)

# Check if a np.object_ array can be converted to np.str_.
if array.dtype == np.object_:
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.