Skip to content

TST: query with timezone aware index & column #34021

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

Merged
merged 10 commits into from
May 20, 2020
15 changes: 15 additions & 0 deletions pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,21 @@ def test_inf(self):
result = df.query(q, engine=self.engine, parser=self.parser)
tm.assert_frame_equal(result, expected)

def test_check_tz_aware_index_query(self, tz_aware_fixture):
# https://github.com/pandas-dev/pandas/issues/29463
tz = tz_aware_fixture
df_index = pd.date_range(
start="2019-01-01", freq="1d", periods=10, tz=tz, name="time"
)
expected = pd.DataFrame(index=df_index)
df = pd.DataFrame(index=df_index)
result = df.query('"2018-01-03 00:00:00+00" < time')
Copy link
Member

Choose a reason for hiding this comment

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

Like this, time is always greater than 2018-01-03 00:00:00+00 - I think @mroeschke was suggesting to use, say, 2019-01-04 in expected, 2019-01-01 in df, and then 2019-01-03 in query

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MarcoGorelli Thank you for the comment / appreciation.

If we use 2019-01-03 in query it results in earlier problem , because the shape of "expected" and "df" remains the same also their tz is changing , but in query the tz remains constant and when we query it to diff timezone their shape becomes inconsistent as the query tz is always constant. (So it always result in +1 or -1 records and hence fails.)

Hence I think he suggested to use 2018-01-03 , as it will always result in consistent shape while checking tz-aware for all time zones.

Screenshot 2020-05-07 at 02 01 14

Copy link
Member

Choose a reason for hiding this comment

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

Ah sorry I'd misunderstood, thanks!

Copy link
Member

Choose a reason for hiding this comment

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

In that case, it seems that your index for both df and expected are identical - if so, can you make a variable to share them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done ! Thanks.

tm.assert_frame_equal(result, expected)

Copy link
Contributor

Choose a reason for hiding this comment

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

can you also test the working case here, e.g. result = df.reset_index().query(....)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jreback Done. Please review.

Copy link
Member

Choose a reason for hiding this comment

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

@vipinkjonwal thanks for your updates

Like this, what's the difference between df2.set_index("time") and df?

I think Jeff meant

result = df.reset_index().query('"2018-01-03 00:00:00+00" < time')
expected = pd.DataFrame(df_index)
tm.assert_frame_equal(result, expected)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MarcoGorelli , I thought this was already working when the related issue was raised , I've updated the test with suggested changes.

expected = pd.DataFrame(df_index)
result = df.reset_index().query('"2018-01-03 00:00:00+00" < time')
tm.assert_frame_equal(result, expected)


@td.skip_if_no_ne
class TestDataFrameQueryNumExprPython(TestDataFrameQueryNumExprPandas):
Expand Down