Skip to content

COMPAT: implement visit_Constant for 3.8 compat #28101

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 8 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Sparse
Other
^^^^^

-
- Compatibility with Python 3.8 in :meth:`DataFrame.query` (:issue:`27261`)
-

.. _whatsnew_0.252.contributors:
Expand Down
1 change: 1 addition & 0 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
PY35 = sys.version_info[:2] == (3, 5)
PY36 = sys.version_info >= (3, 6)
PY37 = sys.version_info >= (3, 7)
PY38 = sys.version_info >= (3, 8)
PYPY = platform.python_implementation() == "PyPy"


Expand Down
3 changes: 3 additions & 0 deletions pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,9 @@ def visit_NameConstant(self, node, **kwargs):
def visit_Num(self, node, **kwargs):
return self.const_type(node.n, self.env)

def visit_Constant(self, node, **kwargs):
return self.const_type(node.n, self.env)

def visit_Str(self, node, **kwargs):
name = self.env.add_tmp(node.s)
return self.term_type(name, self.env)
Expand Down
27 changes: 25 additions & 2 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pandas.core.dtypes.common import is_bool, is_list_like, is_scalar

import pandas as pd
from pandas import DataFrame, Series, date_range
from pandas import DataFrame, Series, compat, date_range
from pandas.core.computation import pytables
from pandas.core.computation.check import _NUMEXPR_VERSION
from pandas.core.computation.engines import NumExprClobberingError, _engines
Expand Down Expand Up @@ -1267,7 +1267,10 @@ def test_assignment_column(self):
msg = "left hand side of an assignment must be a single name"
with pytest.raises(SyntaxError, match=msg):
df.eval("d,c = a + b")
msg = "can't assign to function call"
if compat.PY38:
msg = "cannot assign to function call"
else:
msg = "can't assign to function call"
with pytest.raises(SyntaxError, match=msg):
df.eval('Timestamp("20131001") = a + b')

Expand Down Expand Up @@ -1967,6 +1970,26 @@ def test_bool_ops_fails_on_scalars(lhs, cmp, rhs, engine, parser):
pd.eval(ex, engine=engine, parser=parser)


@pytest.mark.parametrize(
"other",
[
"'x'",
pytest.param(
"...", marks=pytest.mark.xfail(not compat.PY38, reason="GH-28116")
),
],
)
def test_equals_various(other):
df = DataFrame({"A": ["a", "b", "c"]})
result = df.eval("A == {}".format(other))
expected = Series([False, False, False], name="A")
if _USE_NUMEXPR:
# https://github.com/pandas-dev/pandas/issues/10239
# lose name with numexpr engine. Remove when that's fixed.
expected.name = None
tm.assert_series_equal(result, expected)


def test_inf(engine, parser):
s = "inf + 1"
expected = np.inf
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/io/parser/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1898,7 +1898,10 @@ def test_null_byte_char(all_parsers):
out = parser.read_csv(StringIO(data), names=names)
tm.assert_frame_equal(out, expected)
else:
msg = "NULL byte detected"
if compat.PY38:
msg = "line contains NUL"
else:
msg = "NULL byte detected"
with pytest.raises(ParserError, match=msg):
parser.read_csv(StringIO(data), names=names)

Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/scalar/test_nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def _get_overlap_public_nat_methods(klass, as_tuple=False):
"day_name",
"dst",
"floor",
"fromisocalendar",
"fromisoformat",
"fromordinal",
"fromtimestamp",
Expand Down Expand Up @@ -296,6 +297,8 @@ def test_overlap_public_nat_methods(klass, expected):
# "fromisoformat" was introduced in 3.7
if klass is Timestamp and not compat.PY37:
expected.remove("fromisoformat")
if klass is Timestamp and not compat.PY38:
expected.remove("fromisocalendar")

assert _get_overlap_public_nat_methods(klass) == expected

Expand Down