Skip to content
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

Preserve coordinate dtypes #178

Merged
merged 5 commits into from
Jan 3, 2024
Merged

Preserve coordinate dtypes #178

merged 5 commits into from
Jan 3, 2024

Conversation

nvictus
Copy link
Member

@nvictus nvictus commented Dec 23, 2023

Changes overlap to preserve the underlying integer type of the coordinate columns when casting output to nullable pandas dtypes.

EDIT: Changed to least surprising behavior:

  1. Coordinate (start and end) columns in the input dataframes preserve dtypes in the output dataframe, when possible.
  2. Outer join operations that introduce null values into coordinate columns will exhibit the native casting behavior of the column dtype: i.e. numpy columns get cast to float64 with NaNs while nullable pandas arrays remain unchanged (e.g. UInt32 -> UInt32) and contain NAs.

If one is worried about input-dependent casting behavior, one can convert coord columns to nullable types before passing to overlap with the.convert_dtypes() method. float64 output columns can also be forced into nullable Int64 after overlap by applying .convert_dtypes() on the output.

@gfudenberg
Copy link
Member

sounds nice! should we add something like the following to tests?

df1.astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()})
df2.astype({"start": uint32 "end": uint32})

overlap_dtypes = bioframe.overlap(df1, df2).dtypes
assert (df1.dtypes = overlap_dtypes[:3]).all()
assert (df2.dtypes = overlap_dtypes[3:]).all()

@nvictus
Copy link
Member Author

nvictus commented Jan 1, 2024

Added tests.

@nvictus nvictus changed the title Convert coords to appropriate nullable dtype Preserve coordinate dtypes Jan 1, 2024
Copy link
Member

@gfudenberg gfudenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice dtype test!

The only question I have is if the dtype conversions should go into a
Notes ---- section of the overlap docstring

@nvictus
Copy link
Member Author

nvictus commented Jan 3, 2024

Added the note, and introduced an ensure_nullable option to force upcast to nullable dtypes in the input, which ensures the output coord columns are nullable (+ tests). Saves a few keystrokes from having to do it manually before applying overlap.

@nvictus nvictus merged commit 275a311 into open2c:main Jan 3, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants