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

fixing open-ended adjustements error #216

Merged
merged 7 commits into from
Dec 20, 2023

Conversation

BaptisteVandecrux
Copy link
Member

While trying to prescribe an open-ended adjustments, I noticed that it currently causes an error.

When we read the start end end times of adjustments, they receive a time zone info due to their ISO format. The AWS xarray dataset does not have a time zone info (because of an xarray limitation). So the timezone info is removed from the adjustments time bounds (l.183-184).

What was missing is that when start or end date of adjustments are blank (meaning open-start, open-ended bounds), we use a timestamp (then time-zone-naive) from the AWS dataset, and that it then causes an error later on when trying to remove the time-zone info from these same time-zone-naive bounds.

While trying to prescribe an open-ended adjustments, I noticed that it currently causes an error.

When we read the start end end times of adjustments, they receive a time zone info due to their ISO format. The AWS xarray dataset does not have a time zone info (because of an [xarray limitation](pydata/xarray#3291)). So the timezone info is removed from the adjustments time bounds (l.183-184).

What was missing is that when start or end date of adjustments are blank (meaning open-start, open-ended bounds), we use a timestamp (then time-zone-naive) from the AWS dataset, and that it then causes an error later on when trying to remove the time-zone info from these same time-zone-naive bounds.
switching copies to deep copies
Copy link
Contributor

@ladsmund ladsmund left a comment

Choose a reason for hiding this comment

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

The PR looks like it solves the problem.
Note: there is a bug related to non-UTC adjustment timecodes.

I have sugested a more simple solution to the open ended intervals.

src/pypromice/qc/github_data_issues.py Outdated Show resolved Hide resolved
src/pypromice/qc/github_data_issues.py Outdated Show resolved Hide resolved
ladsmund
ladsmund previously approved these changes Dec 19, 2023
Comment on lines +180 to +182
adj_info[['t0','t1']] = adj_info[['t0','t1']].astype(object)
adj_info.loc[adj_info.t1.isnull()|(adj_info.t1==''), "t1"] = None
adj_info.loc[adj_info.t0.isnull()|(adj_info.t0==''), "t0"] = None
Copy link
Contributor

Choose a reason for hiding this comment

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

t0 and t1 are already checked in line 219:222 except for the empty string and np.nan
Consider instead

                if isinstance(t0, str) and t0 != '':
                    t0 = pd.to_datetime(t0, utc=True).tz_localize(None)
                else:
                    t0 = None
                if isinstance(t1, str) and t1 != '':
                    t1 = pd.to_datetime(t1, utc=True).tz_localize(None)
                else:
                    t1 = None

Copy link
Member Author

Choose a reason for hiding this comment

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

Not a big fan of if/else in for loop. I'd rather prepare the adj_info table before looping through it.
Now I see that I have used part of your suggestion l.219-222 so I admit it is currently a bit hybrid.

adj_info.t1 = pd.to_datetime(adj_info.t1).dt.tz_localize(None)

# making sure that t0 and t1 columns are object dtype then replaceing nan with None
adj_info[['t0','t1']] = adj_info[['t0','t1']].astype(object)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand why you cast the type to object. I suppose the dynamically inferred types are either

  • str if all the values in the columns are strings
  • float if all the values in the columns inferred as nan
  • object if the values in the columns have different types

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess that, along with your next comment, it is a matter of personal preference:

I prefer replacing all the missing dates by None in two single-line vectorized call (l.181-182) rather than having ifs in a for loop. But before I do that, I need to make sure that the t0 and t1 columns can accommodate None. If a t0 or t1 column has a float type and I try to replace the values by None pandas actually coerces those None into their float equivalent: np.nan. This causes problem later as slice(np.nan, np.nan) fails. When t0 and t1 are of object type, then replacing some of their values by None will keep the None as NoneType.

@BaptisteVandecrux BaptisteVandecrux merged commit a7997ef into main Dec 20, 2023
4 checks passed
@BaptisteVandecrux BaptisteVandecrux deleted the fixing-open-bounds-adjustements branch December 20, 2023 08:38
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