Skip to content

Commit 44164ce

Browse files
committed
Merge remote-tracking branch 'upstream/main' into add-dataframe-pyarrow-column-dtype-datetime-test
2 parents 4812f69 + 45aa7a5 commit 44164ce

27 files changed

+98
-87
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ I/O
702702
- Bug in :meth:`read_stata` raising ``KeyError`` when input file is stored in big-endian format and contains strL data. (:issue:`58638`)
703703
- Bug in :meth:`read_stata` where extreme value integers were incorrectly interpreted as missing for format versions 111 and prior (:issue:`58130`)
704704
- Bug in :meth:`read_stata` where the missing code for double was not recognised for format versions 105 and prior (:issue:`58149`)
705+
- Bug in :meth:`set_option` where setting the pandas option ``display.html.use_mathjax`` to ``False`` has no effect (:issue:`59884`)
705706
- Bug in :meth:`to_excel` where :class:`MultiIndex` columns would be merged to a single row when ``merge_cells=False`` is passed (:issue:`60274`)
706707

707708
Period

pandas/core/dtypes/cast.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,7 @@ def convert_dtypes(
11621162

11631163
def maybe_infer_to_datetimelike(
11641164
value: npt.NDArray[np.object_],
1165+
convert_to_nullable_dtype: bool = False,
11651166
) -> np.ndarray | DatetimeArray | TimedeltaArray | PeriodArray | IntervalArray:
11661167
"""
11671168
we might have a array (or single object) that is datetime like,
@@ -1199,6 +1200,7 @@ def maybe_infer_to_datetimelike(
11991200
# numpy would have done it for us.
12001201
convert_numeric=False,
12011202
convert_non_numeric=True,
1203+
convert_to_nullable_dtype=convert_to_nullable_dtype,
12021204
dtype_if_all_nat=np.dtype("M8[s]"),
12031205
)
12041206

pandas/core/frame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,8 +2115,8 @@ def from_records(
21152115
"""
21162116
Convert structured or record ndarray to DataFrame.
21172117
2118-
Creates a DataFrame object from a structured ndarray, sequence of
2119-
tuples or dicts, or DataFrame.
2118+
Creates a DataFrame object from a structured ndarray, or sequence of
2119+
tuples or dicts.
21202120
21212121
Parameters
21222122
----------

pandas/core/internals/blocks.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
PeriodArray,
109109
TimedeltaArray,
110110
)
111+
from pandas.core.arrays.string_ import StringDtype
111112
from pandas.core.base import PandasObject
112113
import pandas.core.common as com
113114
from pandas.core.computation import expressions
@@ -1336,7 +1337,7 @@ def fillna(
13361337
return [self.copy(deep=False)]
13371338

13381339
if limit is not None:
1339-
mask[mask.cumsum(self.ndim - 1) > limit] = False
1340+
mask[mask.cumsum(self.values.ndim - 1) > limit] = False
13401341

13411342
if inplace:
13421343
nbs = self.putmask(mask.T, value)
@@ -1684,7 +1685,7 @@ def where(self, other, cond) -> list[Block]:
16841685
res_values = arr._where(cond, other).T
16851686
except (ValueError, TypeError):
16861687
if self.ndim == 1 or self.shape[0] == 1:
1687-
if isinstance(self.dtype, IntervalDtype):
1688+
if isinstance(self.dtype, (IntervalDtype, StringDtype)):
16881689
# TestSetitemFloatIntervalWithIntIntervalValues
16891690
blk = self.coerce_to_target_dtype(orig_other, raise_on_upcast=False)
16901691
return blk.where(orig_other, orig_cond)
@@ -1854,9 +1855,9 @@ def fillna(
18541855
limit: int | None = None,
18551856
inplace: bool = False,
18561857
) -> list[Block]:
1857-
if isinstance(self.dtype, IntervalDtype):
1858+
if isinstance(self.dtype, (IntervalDtype, StringDtype)):
18581859
# Block.fillna handles coercion (test_fillna_interval)
1859-
if limit is not None:
1860+
if isinstance(self.dtype, IntervalDtype) and limit is not None:
18601861
raise ValueError("limit must be None")
18611862
return super().fillna(
18621863
value=value,

pandas/core/internals/construction.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,9 @@ def convert(arr):
966966
if dtype is None:
967967
if arr.dtype == np.dtype("O"):
968968
# i.e. maybe_convert_objects didn't convert
969-
arr = maybe_infer_to_datetimelike(arr)
970-
if dtype_backend != "numpy" and arr.dtype == np.dtype("O"):
969+
convert_to_nullable_dtype = dtype_backend != "numpy"
970+
arr = maybe_infer_to_datetimelike(arr, convert_to_nullable_dtype)
971+
if convert_to_nullable_dtype and arr.dtype == np.dtype("O"):
971972
new_dtype = StringDtype()
972973
arr_cls = new_dtype.construct_array_type()
973974
arr = arr_cls._from_sequence(arr, dtype=new_dtype)

pandas/io/formats/html.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ def _write_table(self, indent: int = 0) -> None:
241241
use_mathjax = get_option("display.html.use_mathjax")
242242
if not use_mathjax:
243243
_classes.append("tex2jax_ignore")
244+
_classes.append("mathjax_ignore")
244245
if self.classes is not None:
245246
if isinstance(self.classes, str):
246247
self.classes = self.classes.split()

pandas/io/formats/style_render.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,11 @@ def _translate(
366366
if not get_option("styler.html.mathjax"):
367367
table_attr = table_attr or ""
368368
if 'class="' in table_attr:
369-
table_attr = table_attr.replace('class="', 'class="tex2jax_ignore ')
369+
table_attr = table_attr.replace(
370+
'class="', 'class="tex2jax_ignore mathjax_ignore '
371+
)
370372
else:
371-
table_attr += ' class="tex2jax_ignore"'
373+
table_attr += ' class="tex2jax_ignore mathjax_ignore"'
372374
d.update({"table_attributes": table_attr})
373375

374376
if self.tooltips:

pandas/io/sql.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
from pandas.core.dtypes.common import (
4646
is_dict_like,
4747
is_list_like,
48+
is_object_dtype,
49+
is_string_dtype,
4850
)
4951
from pandas.core.dtypes.dtypes import (
5052
ArrowDtype,
@@ -58,6 +60,7 @@
5860
Series,
5961
)
6062
from pandas.core.arrays import ArrowExtensionArray
63+
from pandas.core.arrays.string_ import StringDtype
6164
from pandas.core.base import PandasObject
6265
import pandas.core.common as com
6366
from pandas.core.common import maybe_make_list
@@ -1316,7 +1319,12 @@ def _harmonize_columns(
13161319
elif dtype_backend == "numpy" and col_type is float:
13171320
# floats support NA, can always convert!
13181321
self.frame[col_name] = df_col.astype(col_type)
1319-
1322+
elif (
1323+
using_string_dtype()
1324+
and is_string_dtype(col_type)
1325+
and is_object_dtype(self.frame[col_name])
1326+
):
1327+
self.frame[col_name] = df_col.astype(col_type)
13201328
elif dtype_backend == "numpy" and len(df_col) == df_col.count():
13211329
# No NA values, can convert ints and bools
13221330
if col_type is np.dtype("int64") or col_type is bool:
@@ -1403,6 +1411,7 @@ def _get_dtype(self, sqltype):
14031411
DateTime,
14041412
Float,
14051413
Integer,
1414+
String,
14061415
)
14071416

14081417
if isinstance(sqltype, Float):
@@ -1422,6 +1431,10 @@ def _get_dtype(self, sqltype):
14221431
return date
14231432
elif isinstance(sqltype, Boolean):
14241433
return bool
1434+
elif isinstance(sqltype, String):
1435+
if using_string_dtype():
1436+
return StringDtype(na_value=np.nan)
1437+
14251438
return object
14261439

14271440

@@ -2205,7 +2218,7 @@ def read_table(
22052218
elif using_string_dtype():
22062219
from pandas.io._util import arrow_string_types_mapper
22072220

2208-
arrow_string_types_mapper()
2221+
mapping = arrow_string_types_mapper()
22092222
else:
22102223
mapping = None
22112224

@@ -2286,6 +2299,10 @@ def read_query(
22862299
from pandas.io._util import _arrow_dtype_mapping
22872300

22882301
mapping = _arrow_dtype_mapping().get
2302+
elif using_string_dtype():
2303+
from pandas.io._util import arrow_string_types_mapper
2304+
2305+
mapping = arrow_string_types_mapper()
22892306
else:
22902307
mapping = None
22912308

pandas/tests/frame/indexing/test_where.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,15 +1025,9 @@ def test_where_producing_ea_cond_for_np_dtype():
10251025
@pytest.mark.parametrize(
10261026
"replacement", [0.001, True, "snake", None, datetime(2022, 5, 4)]
10271027
)
1028-
def test_where_int_overflow(replacement, using_infer_string):
1028+
def test_where_int_overflow(replacement):
10291029
# GH 31687
10301030
df = DataFrame([[1.0, 2e25, "nine"], [np.nan, 0.1, None]])
1031-
if using_infer_string and replacement not in (None, "snake"):
1032-
with pytest.raises(
1033-
TypeError, match=f"Invalid value '{replacement}' for dtype 'str'"
1034-
):
1035-
df.where(pd.notnull(df), replacement)
1036-
return
10371031
result = df.where(pd.notnull(df), replacement)
10381032
expected = DataFrame([[1.0, 2e25, "nine"], [replacement, 0.1, replacement]])
10391033

pandas/tests/groupby/aggregate/test_aggregate.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import numpy as np
1010
import pytest
1111

12-
from pandas._config import using_string_dtype
13-
1412
from pandas.errors import SpecificationError
1513

1614
from pandas.core.dtypes.common import is_integer_dtype
@@ -296,12 +294,11 @@ def aggfun_1(ser):
296294
assert len(result) == 0
297295

298296

299-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
300297
def test_wrap_agg_out(three_group):
301298
grouped = three_group.groupby(["A", "B"])
302299

303300
def func(ser):
304-
if ser.dtype == object:
301+
if ser.dtype == object or ser.dtype == "string":
305302
raise TypeError("Test error message")
306303
return ser.sum()
307304

@@ -1117,7 +1114,6 @@ def test_lambda_named_agg(func):
11171114
tm.assert_frame_equal(result, expected)
11181115

11191116

1120-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
11211117
def test_aggregate_mixed_types():
11221118
# GH 16916
11231119
df = DataFrame(
@@ -1129,7 +1125,7 @@ def test_aggregate_mixed_types():
11291125
expected = DataFrame(
11301126
expected_data,
11311127
index=Index([2, "group 1"], dtype="object", name="grouping"),
1132-
columns=Index(["X", "Y", "Z"], dtype="object"),
1128+
columns=Index(["X", "Y", "Z"]),
11331129
)
11341130
tm.assert_frame_equal(result, expected)
11351131

0 commit comments

Comments
 (0)