Skip to content

Automatically name index#1974

Merged
visr merged 2 commits into
mainfrom
index-name
Dec 16, 2024
Merged

Automatically name index#1974
visr merged 2 commits into
mainfrom
index-name

Conversation

@visr
Copy link
Copy Markdown
Member

@visr visr commented Dec 13, 2024

Fixes #1968

We let pandera check the index name with check_name = True. This adds a pandera parser called _name_index to set the index to the desired name before validating. This still keeps the index names, but offers more user convenience, since the index name is lost with many pandas operations.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

These are no longer needed because the parser will rename it for us.

Comment on lines +19 to +24
@pa.dataframe_parser
def _name_index(cls, df):
# Node and Edge have different index names, avoid running both parsers
if cls.__name__ not in ("NodeSchema", "EdgeSchema"):
df.index.name = "fid"
return df
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I was confused for a while since I expected the methods in the subclasses in edge.py and node.py to override this. But when debugging a test failure I saw that first the node parser was applied, naming it "node_id", and then this one, making it end up with the wrong name. Hence the check for those class names.

Copy link
Copy Markdown
Member

@evetion evetion Dec 13, 2024

Choose a reason for hiding this comment

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

How about df.index.name = cls._index_name()? A mix of both old and new. Needing to check specific schemas in the generic implementation is bad.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah we could go with that as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in 007ee67

Comment thread python/ribasim/ribasim/schemas.py Outdated

class BasinConcentrationExternalSchema(_BaseSchema):
fid: Index[Int32] = pa.Field(default=1, check_name=True, coerce=True)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

gen_python.jl creates even more whitespace, of which ruff now seems to leave one. I tried to fix this with OteraEngine settings but didn't manage. Not so important though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm the codegen CI doesn't generate the whitespace, so I removed it again. Not sure why I get different results.

@visr visr requested a review from evetion December 13, 2024 10:52
Copy link
Copy Markdown
Member

@evetion evetion left a comment

Choose a reason for hiding this comment

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

Are there any Ribasim operations changing the DataFrame index? This is more lenient I guess. Maybe add a debug statement if the index name doesn't match?

Comment on lines +19 to +24
@pa.dataframe_parser
def _name_index(cls, df):
# Node and Edge have different index names, avoid running both parsers
if cls.__name__ not in ("NodeSchema", "EdgeSchema"):
df.index.name = "fid"
return df
Copy link
Copy Markdown
Member

@evetion evetion Dec 13, 2024

Choose a reason for hiding this comment

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

How about df.index.name = cls._index_name()? A mix of both old and new. Needing to check specific schemas in the generic implementation is bad.

@visr
Copy link
Copy Markdown
Member Author

visr commented Dec 13, 2024

Are there any Ribasim operations changing the DataFrame index? This is more lenient I guess. Maybe add a debug statement if the index name doesn't match?

Not that I'm aware. I don't understand exactly what kind of debug statement you had in mind.

@evetion
Copy link
Copy Markdown
Member

evetion commented Dec 13, 2024

Are there any Ribasim operations changing the DataFrame index? This is more lenient I guess. Maybe add a debug statement if the index name doesn't match?

Not that I'm aware. I don't understand exactly what kind of debug statement you had in mind.

Then this PR is really for external users that are playing fast and loose with their dataframes right (not using the add API)?

pseudo for debug statement:

if df.index.name != cls.index_name():
  logging.debug("How dare you")
  df.index.name = cls.index_name()

@visr
Copy link
Copy Markdown
Member Author

visr commented Dec 13, 2024

Hmm in this PR I actually removed two explicit index namings because we can now rely on this. I don't think a debug statement then makes sense if we say we'll take care of it.

Here you can see a bunch of uses: https://github.com/search?q=repo%3ADeltares%2FRibasim-NL+index.name+%3D&type=code
True that you wouldn't need it with just add API, but you currently can't do everything we need to do like that, so making an effort to make the direct df API usable IMO isn't so bad.

Copy link
Copy Markdown
Member

@evetion evetion left a comment

Choose a reason for hiding this comment

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

Neat!

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.

Don't require index.name to be set to fid

2 participants