Skip to content

Commit 9751092

Browse files
Merge remote-tracking branch 'upstream/master' into bisect
2 parents 69c2dc4 + ad19057 commit 9751092

File tree

23 files changed

+186
-104
lines changed

23 files changed

+186
-104
lines changed

.github/workflows/posix.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
COVERAGE: ${{ !contains(matrix.settings[0], 'pypy') }}
5353
concurrency:
5454
# https://github.community/t/concurrecy-not-work-for-push/183068/7
55-
group: ${{ github.event_name == 'push' && github.run_number || github.ref }}-${{ matrix.settings[0] }}
55+
group: ${{ github.event_name == 'push' && github.run_number || github.ref }}-${{ matrix.settings[0] }}-${{ matrix.settings[1] }}
5656
cancel-in-progress: true
5757

5858
services:

doc/source/user_guide/io.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,7 @@ function takes a number of arguments. Only the first is required.
18511851
* ``mode`` : Python write mode, default 'w'
18521852
* ``encoding``: a string representing the encoding to use if the contents are
18531853
non-ASCII, for Python versions prior to 3
1854-
* ``line_terminator``: Character sequence denoting line end (default ``os.linesep``)
1854+
* ``lineterminator``: Character sequence denoting line end (default ``os.linesep``)
18551855
* ``quoting``: Set quoting rules as in csv module (default csv.QUOTE_MINIMAL). Note that if you have set a ``float_format`` then floats are converted to strings and csv.QUOTE_NONNUMERIC will treat them as non-numeric
18561856
* ``quotechar``: Character used to quote fields (default '"')
18571857
* ``doublequote``: Control quoting of ``quotechar`` in fields (default True)

doc/source/user_guide/style.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@
11001100
" - [.highlight_null][nullfunc]: for use with identifying missing data. \n",
11011101
" - [.highlight_min][minfunc] and [.highlight_max][maxfunc]: for use with identifying extremeties in data.\n",
11021102
" - [.highlight_between][betweenfunc] and [.highlight_quantile][quantilefunc]: for use with identifying classes within data.\n",
1103-
" - [.background_gradient][bgfunc]: a flexible method for highlighting cells based or their, or other, values on a numeric scale.\n",
1103+
" - [.background_gradient][bgfunc]: a flexible method for highlighting cells based on their, or other, values on a numeric scale.\n",
11041104
" - [.text_gradient][textfunc]: similar method for highlighting text based on their, or other, values on a numeric scale.\n",
11051105
" - [.bar][barfunc]: to display mini-charts within cell backgrounds.\n",
11061106
" \n",

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ Datetimelike
724724
- ``np.maximum.reduce`` and ``np.minimum.reduce`` now correctly return :class:`Timestamp` and :class:`Timedelta` objects when operating on :class:`Series`, :class:`DataFrame`, or :class:`Index` with ``datetime64[ns]`` or ``timedelta64[ns]`` dtype (:issue:`43923`)
725725
- Bug in adding a ``np.timedelta64`` object to a :class:`BusinessDay` or :class:`CustomBusinessDay` object incorrectly raising (:issue:`44532`)
726726
- Bug in :meth:`Index.insert` for inserting ``np.datetime64``, ``np.timedelta64`` or ``tuple`` into :class:`Index` with ``dtype='object'`` with negative loc adding ``None`` and replacing existing value (:issue:`44509`)
727+
- Bug in :meth:`Timestamp.to_pydatetime` failing to retain the ``fold`` attribute (:issue:`45087`)
727728
- Bug in :meth:`Series.mode` with ``DatetimeTZDtype`` incorrectly returning timezone-naive and ``PeriodDtype`` incorrectly raising (:issue:`41927`)
728729
- Bug in :class:`DateOffset`` addition with :class:`Timestamp` where ``offset.nanoseconds`` would not be included in the result (:issue:`43968`, :issue:`36589`)
729730
- Bug in :meth:`Timestamp.fromtimestamp` not supporting the ``tz`` argument (:issue:`45083`)

doc/source/whatsnew/v1.5.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Other API changes
9494

9595
Deprecations
9696
~~~~~~~~~~~~
97-
-
97+
- Deprecated the keyword ``line_terminator`` in :meth:`DataFrame.to_csv` and :meth:`Series.to_csv`, use ``lineterminator`` instead; this is for consistency with :func:`read_csv` and the standard library 'csv' module (:issue:`9568`)
9898
-
9999

100100
.. ---------------------------------------------------------------------------
@@ -119,6 +119,7 @@ Categorical
119119
Datetimelike
120120
^^^^^^^^^^^^
121121
- Bug in :func:`to_datetime` with sequences of ``np.str_`` objects incorrectly raising (:issue:`32264`)
122+
- Bug in :class:`Timestamp` construction when passing datetime components as positional arguments and ``tzinfo`` as a keyword argument incorrectly raising (:issue:`31929`)
122123
-
123124

124125
Timedelta

pandas/_libs/src/parser/tokenizer.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ static int parser_buffer_bytes(parser_t *self, size_t nbytes,
649649
#define END_LINE() END_LINE_STATE(START_RECORD)
650650

651651
#define IS_TERMINATOR(c) \
652-
(c == line_terminator)
652+
(c == lineterminator)
653653

654654
#define IS_QUOTE(c) ((c == self->quotechar && self->quoting != QUOTE_NONE))
655655

@@ -718,7 +718,7 @@ int tokenize_bytes(parser_t *self,
718718
char *stream;
719719
char *buf = self->data + self->datapos;
720720

721-
const char line_terminator = (self->lineterminator == '\0') ?
721+
const char lineterminator = (self->lineterminator == '\0') ?
722722
'\n' : self->lineterminator;
723723

724724
// 1000 is something that couldn't fit in "char"

pandas/_libs/tslibs/timestamps.pyx

+22-1
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ cdef class _Timestamp(ABCTimestamp):
898898

899899
return datetime(self.year, self.month, self.day,
900900
self.hour, self.minute, self.second,
901-
self.microsecond, self.tzinfo)
901+
self.microsecond, self.tzinfo, fold=self.fold)
902902

903903
cpdef to_datetime64(self):
904904
"""
@@ -1295,6 +1295,27 @@ class Timestamp(_Timestamp):
12951295
# GH#17690 tzinfo must be a datetime.tzinfo object, ensured
12961296
# by the cython annotation.
12971297
if tz is not None:
1298+
if (is_integer_object(tz)
1299+
and is_integer_object(ts_input)
1300+
and is_integer_object(freq)
1301+
):
1302+
# GH#31929 e.g. Timestamp(2019, 3, 4, 5, 6, tzinfo=foo)
1303+
# TODO(GH#45307): this will still be fragile to
1304+
# mixed-and-matched positional/keyword arguments
1305+
ts_input = datetime(
1306+
ts_input,
1307+
freq,
1308+
tz,
1309+
unit or 0,
1310+
year or 0,
1311+
month or 0,
1312+
day or 0,
1313+
fold=fold or 0,
1314+
)
1315+
nanosecond = hour
1316+
tz = tzinfo
1317+
return cls(ts_input, nanosecond=nanosecond, tz=tz)
1318+
12981319
raise ValueError('Can provide at most one of tz, tzinfo')
12991320

13001321
# User passed tzinfo instead of tz; avoid silently ignoring

pandas/core/dtypes/cast.py

+52
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,58 @@ def ensure_nanosecond_dtype(dtype: DtypeObj) -> DtypeObj:
14181418
return dtype
14191419

14201420

1421+
# TODO: other value-dependent functions to standardize here include
1422+
# dtypes.concat.cast_to_common_type and Index._find_common_type_compat
1423+
def find_result_type(left: ArrayLike, right: Any) -> DtypeObj:
1424+
"""
1425+
Find the type/dtype for a the result of an operation between these objects.
1426+
1427+
This is similar to find_common_type, but looks at the objects instead
1428+
of just their dtypes. This can be useful in particular when one of the
1429+
objects does not have a `dtype`.
1430+
1431+
Parameters
1432+
----------
1433+
left : np.ndarray or ExtensionArray
1434+
right : Any
1435+
1436+
Returns
1437+
-------
1438+
np.dtype or ExtensionDtype
1439+
1440+
See also
1441+
--------
1442+
find_common_type
1443+
numpy.result_type
1444+
"""
1445+
new_dtype: DtypeObj
1446+
1447+
if left.dtype.kind in ["i", "u", "c"] and (
1448+
lib.is_integer(right) or lib.is_float(right)
1449+
):
1450+
# e.g. with int8 dtype and right=512, we want to end up with
1451+
# np.int16, whereas infer_dtype_from(512) gives np.int64,
1452+
# which will make us upcast too far.
1453+
if lib.is_float(right) and right.is_integer() and left.dtype.kind != "f":
1454+
right = int(right)
1455+
1456+
# Argument 1 to "result_type" has incompatible type "Union[ExtensionArray,
1457+
# ndarray[Any, Any]]"; expected "Union[Union[_SupportsArray[dtype[Any]],
1458+
# _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex,
1459+
# str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]],
1460+
# Union[dtype[Any], None, Type[Any], _SupportsDType[dtype[Any]], str,
1461+
# Union[Tuple[Any, int], Tuple[Any, Union[SupportsIndex,
1462+
# Sequence[SupportsIndex]]], List[Any], _DTypeDict, Tuple[Any, Any]]]]"
1463+
new_dtype = np.result_type(left, right) # type:ignore[arg-type]
1464+
1465+
else:
1466+
dtype, _ = infer_dtype_from(right, pandas_dtype=True)
1467+
1468+
new_dtype = find_common_type([left.dtype, dtype])
1469+
1470+
return new_dtype
1471+
1472+
14211473
@overload
14221474
def find_common_type(types: list[np.dtype]) -> np.dtype:
14231475
...

pandas/core/generic.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
InvalidIndexError,
6868
)
6969
from pandas.util._decorators import (
70+
deprecate_kwarg,
7071
doc,
7172
rewrite_axis_style_signature,
7273
)
@@ -3355,6 +3356,7 @@ def to_latex(
33553356
storage_options=_shared_docs["storage_options"],
33563357
compression_options=_shared_docs["compression_options"],
33573358
)
3359+
@deprecate_kwarg(old_arg_name="line_terminator", new_arg_name="lineterminator")
33583360
def to_csv(
33593361
self,
33603362
path_or_buf: FilePath | WriteBuffer[bytes] | WriteBuffer[str] | None = None,
@@ -3370,7 +3372,7 @@ def to_csv(
33703372
compression: CompressionOptions = "infer",
33713373
quoting: int | None = None,
33723374
quotechar: str = '"',
3373-
line_terminator: str | None = None,
3375+
lineterminator: str | None = None,
33743376
chunksize: int | None = None,
33753377
date_format: str | None = None,
33763378
doublequote: bool_t = True,
@@ -3449,10 +3451,16 @@ def to_csv(
34493451
will treat them as non-numeric.
34503452
quotechar : str, default '\"'
34513453
String of length 1. Character used to quote fields.
3452-
line_terminator : str, optional
3454+
lineterminator : str, optional
34533455
The newline character or character sequence to use in the output
34543456
file. Defaults to `os.linesep`, which depends on the OS in which
34553457
this method is called ('\\n' for linux, '\\r\\n' for Windows, i.e.).
3458+
3459+
.. versionchanged:: 1.5.0
3460+
3461+
Previously was line_terminator, changed for consistency with
3462+
read_csv and the standard library 'csv' module.
3463+
34563464
chunksize : int or None
34573465
Rows to write at a time.
34583466
date_format : str, default None
@@ -3527,7 +3535,7 @@ def to_csv(
35273535

35283536
return DataFrameRenderer(formatter).to_csv(
35293537
path_or_buf,
3530-
line_terminator=line_terminator,
3538+
lineterminator=lineterminator,
35313539
sep=sep,
35323540
encoding=encoding,
35333541
errors=errors,

pandas/core/internals/blocks.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from pandas.core.dtypes.astype import astype_array_safe
3939
from pandas.core.dtypes.cast import (
4040
can_hold_element,
41-
find_common_type,
41+
find_result_type,
4242
infer_dtype_from,
4343
maybe_downcast_numeric,
4444
maybe_downcast_to_dtype,
@@ -1031,10 +1031,7 @@ def coerce_to_target_dtype(self, other) -> Block:
10311031
we can also safely try to coerce to the same dtype
10321032
and will receive the same block
10331033
"""
1034-
# if we cannot then coerce to object
1035-
dtype, _ = infer_dtype_from(other, pandas_dtype=True)
1036-
1037-
new_dtype = find_common_type([self.dtype, dtype])
1034+
new_dtype = find_result_type(self.values, other)
10381035

10391036
return self.astype(new_dtype, copy=False)
10401037

pandas/io/formats/csvs.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __init__(
5959
errors: str = "strict",
6060
compression: CompressionOptions = "infer",
6161
quoting: int | None = None,
62-
line_terminator: str | None = "\n",
62+
lineterminator: str | None = "\n",
6363
chunksize: int | None = None,
6464
quotechar: str | None = '"',
6565
date_format: str | None = None,
@@ -84,7 +84,7 @@ def __init__(
8484
self.quotechar = self._initialize_quotechar(quotechar)
8585
self.doublequote = doublequote
8686
self.escapechar = escapechar
87-
self.line_terminator = line_terminator or os.linesep
87+
self.lineterminator = lineterminator or os.linesep
8888
self.date_format = date_format
8989
self.cols = self._initialize_columns(cols)
9090
self.chunksize = self._initialize_chunksize(chunksize)
@@ -250,7 +250,7 @@ def save(self) -> None:
250250
# Note: self.encoding is irrelevant here
251251
self.writer = csvlib.writer(
252252
handles.handle,
253-
lineterminator=self.line_terminator,
253+
lineterminator=self.lineterminator,
254254
delimiter=self.sep,
255255
quoting=self.quoting,
256256
doublequote=self.doublequote,

pandas/io/formats/format.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
StorageOptions,
5858
WriteBuffer,
5959
)
60+
from pandas.util._decorators import deprecate_kwarg
6061

6162
from pandas.core.dtypes.common import (
6263
is_categorical_dtype,
@@ -1128,6 +1129,7 @@ def to_string(
11281129
string = string_formatter.to_string()
11291130
return save_to_buffer(string, buf=buf, encoding=encoding)
11301131

1132+
@deprecate_kwarg(old_arg_name="line_terminator", new_arg_name="lineterminator")
11311133
def to_csv(
11321134
self,
11331135
path_or_buf: FilePath | WriteBuffer[bytes] | WriteBuffer[str] | None = None,
@@ -1139,7 +1141,7 @@ def to_csv(
11391141
compression: CompressionOptions = "infer",
11401142
quoting: int | None = None,
11411143
quotechar: str = '"',
1142-
line_terminator: str | None = None,
1144+
lineterminator: str | None = None,
11431145
chunksize: int | None = None,
11441146
date_format: str | None = None,
11451147
doublequote: bool = True,
@@ -1160,7 +1162,7 @@ def to_csv(
11601162

11611163
csv_formatter = CSVFormatter(
11621164
path_or_buf=path_or_buf,
1163-
line_terminator=line_terminator,
1165+
lineterminator=lineterminator,
11641166
sep=sep,
11651167
encoding=encoding,
11661168
errors=errors,

0 commit comments

Comments
 (0)