Skip to content

Commit d0fce64

Browse files
authored
Merge branch 'main' into tguard
2 parents 91e02a5 + 4965c51 commit d0fce64

File tree

10 files changed

+104
-30
lines changed

10 files changed

+104
-30
lines changed

ci/code_checks.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
9191
pandas.Series.size \
9292
pandas.Series.T \
9393
pandas.Series.hasnans \
94-
pandas.Series.to_timestamp \
9594
pandas.Series.to_list \
9695
pandas.Series.__iter__ \
9796
pandas.Series.keys \
@@ -218,7 +217,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
218217
pandas.Period.year \
219218
pandas.Period.asfreq \
220219
pandas.Period.now \
221-
pandas.Period.to_timestamp \
222220
pandas.arrays.PeriodArray \
223221
pandas.Interval.closed \
224222
pandas.Interval.left \
@@ -562,7 +560,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
562560
pandas.DataFrame.swapaxes \
563561
pandas.DataFrame.first_valid_index \
564562
pandas.DataFrame.last_valid_index \
565-
pandas.DataFrame.to_timestamp \
566563
pandas.DataFrame.attrs \
567564
pandas.DataFrame.plot \
568565
pandas.DataFrame.sparse.density \
@@ -576,7 +573,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
576573
$BASE_DIR/scripts/validate_docstrings.py --format=actions --errors=EX02 --ignore_functions \
577574
pandas.DataFrame.plot.line \
578575
pandas.Series.plot.line \
579-
pandas.Timestamp.fromtimestamp \
580576
pandas.api.types.infer_dtype \
581577
pandas.api.types.is_datetime64_any_dtype \
582578
pandas.api.types.is_datetime64_ns_dtype \
@@ -590,7 +586,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
590586
pandas.api.types.is_timedelta64_dtype \
591587
pandas.api.types.is_timedelta64_ns_dtype \
592588
pandas.api.types.is_unsigned_integer_dtype \
593-
pandas.core.groupby.DataFrameGroupBy.take \
594589
pandas.io.formats.style.Styler.concat \
595590
pandas.io.formats.style.Styler.export \
596591
pandas.io.formats.style.Styler.set_td_classes \

pandas/_libs/tslibs/nattype.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ class NaTType(_NaT):
570570
571571
Examples
572572
--------
573-
>>> pd.Timestamp.fromtimestamp(1584199972)
573+
>>> pd.Timestamp.fromtimestamp(1584199972) # doctest: +SKIP
574574
Timestamp('2020-03-14 15:32:52')
575575
576576
Note that the output may change depending on your local time.

pandas/_libs/tslibs/period.pyx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,13 @@ cdef class _Period(PeriodMixin):
18411841
Returns
18421842
-------
18431843
Timestamp
1844+
1845+
Examples
1846+
--------
1847+
>>> period = pd.Period('2023-1-1', freq='D')
1848+
>>> timestamp = period.to_timestamp()
1849+
>>> timestamp
1850+
Timestamp('2023-01-01 00:00:00')
18441851
"""
18451852
how = validate_end_alias(how)
18461853

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,7 @@ class Timestamp(_Timestamp):
14511451
14521452
Examples
14531453
--------
1454-
>>> pd.Timestamp.fromtimestamp(1584199972)
1454+
>>> pd.Timestamp.fromtimestamp(1584199972) # doctest: +SKIP
14551455
Timestamp('2020-03-14 15:32:52')
14561456
14571457
Note that the output may change depending on your local time.

pandas/core/arrays/arrow/array.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
TypeVar,
1212
cast,
1313
)
14-
import warnings
1514

1615
import numpy as np
1716

@@ -890,12 +889,13 @@ def to_numpy(
890889
result = np.empty(len(self), dtype=object)
891890
mask = ~self.isna()
892891
result[mask] = np.asarray(self[mask]._data)
892+
elif self._hasna:
893+
data = self.copy()
894+
data[self.isna()] = na_value
895+
return np.asarray(data._data, dtype=dtype)
893896
else:
894-
with warnings.catch_warnings():
895-
# int dtype with NA raises Warning
896-
warnings.filterwarnings("ignore", category=RuntimeWarning)
897-
result = np.asarray(self._data, dtype=dtype)
898-
if copy or self._hasna:
897+
result = np.asarray(self._data, dtype=dtype)
898+
if copy:
899899
result = result.copy()
900900
if self._hasna:
901901
result[self.isna()] = na_value

pandas/core/frame.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,10 +1396,7 @@ def iterrows(self) -> Iterable[tuple[Hashable, Series]]:
13961396
for k, v in zip(self.index, self.values):
13971397
s = klass(v, index=columns, name=k).__finalize__(self)
13981398
if using_cow and self._mgr.is_single_block:
1399-
s._mgr.blocks[0].refs = self._mgr.blocks[0].refs # type: ignore[union-attr] # noqa
1400-
s._mgr.blocks[0].refs.add_reference( # type: ignore[union-attr]
1401-
s._mgr.blocks[0] # type: ignore[arg-type, union-attr]
1402-
)
1399+
s._mgr.add_references(self._mgr) # type: ignore[arg-type]
14031400
yield k, s
14041401

14051402
def itertuples(
@@ -11014,6 +11011,37 @@ def to_timestamp(
1101411011
-------
1101511012
DataFrame
1101611013
The DataFrame has a DatetimeIndex.
11014+
11015+
Examples
11016+
--------
11017+
>>> idx = pd.PeriodIndex(['2023', '2024'], freq='Y')
11018+
>>> d = {'col1': [1, 2], 'col2': [3, 4]}
11019+
>>> df1 = pd.DataFrame(data=d, index=idx)
11020+
>>> df1
11021+
col1 col2
11022+
2023 1 3
11023+
2024 2 4
11024+
11025+
The resulting timestamps will be at the beginning of the year in this case
11026+
11027+
>>> df1 = df1.to_timestamp()
11028+
>>> df1
11029+
col1 col2
11030+
2023-01-01 1 3
11031+
2024-01-01 2 4
11032+
>>> df1.index
11033+
DatetimeIndex(['2023-01-01', '2024-01-01'], dtype='datetime64[ns]', freq=None)
11034+
11035+
Using `freq` which is the offset that the Timestamps will have
11036+
11037+
>>> df2 = pd.DataFrame(data=d, index=idx)
11038+
>>> df2 = df2.to_timestamp(freq='M')
11039+
>>> df2
11040+
col1 col2
11041+
2023-01-31 1 3
11042+
2024-01-31 2 4
11043+
>>> df2.index
11044+
DatetimeIndex(['2023-01-31', '2024-01-31'], dtype='datetime64[ns]', freq=None)
1101711045
"""
1101811046
new_obj = self.copy(deep=copy)
1101911047

pandas/core/internals/array_manager.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ def set_axis(self, axis: AxisInt, new_labels: Index) -> None:
169169
def get_dtypes(self) -> np.ndarray:
170170
return np.array([arr.dtype for arr in self.arrays], dtype="object")
171171

172+
def add_references(self, mgr: BaseArrayManager) -> None:
173+
"""
174+
Only implemented on the BlockManager level
175+
"""
176+
return
177+
172178
def __getstate__(self):
173179
return self.arrays, self._axes
174180

pandas/core/internals/managers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,17 @@ def _has_no_reference_block(self, blkno: int) -> bool:
247247
"""
248248
return not self.blocks[blkno].refs.has_reference()
249249

250+
def add_references(self, mgr: BaseBlockManager) -> None:
251+
"""
252+
Adds the references from one manager to another. We assume that both
253+
managers have the same block structure.
254+
"""
255+
for i, blk in enumerate(self.blocks):
256+
blk.refs = mgr.blocks[i].refs
257+
# Argument 1 to "add_reference" of "BlockValuesRefs" has incompatible type
258+
# "Block"; expected "SharedBlock"
259+
blk.refs.add_reference(blk) # type: ignore[arg-type]
260+
250261
def get_dtypes(self):
251262
dtypes = np.array([blk.dtype for blk in self.blocks])
252263
return dtypes.take(self.blknos)

pandas/core/series.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5710,6 +5710,35 @@ def to_timestamp(
57105710
Returns
57115711
-------
57125712
Series with DatetimeIndex
5713+
5714+
Examples
5715+
--------
5716+
>>> idx = pd.PeriodIndex(['2023', '2024', '2025'], freq='Y')
5717+
>>> s1 = pd.Series([1, 2, 3], index=idx)
5718+
>>> s1
5719+
2023 1
5720+
2024 2
5721+
2025 3
5722+
Freq: A-DEC, dtype: int64
5723+
5724+
The resulting frequency of the Timestamps is `YearBegin`
5725+
5726+
>>> s1 = s1.to_timestamp()
5727+
>>> s1
5728+
2023-01-01 1
5729+
2024-01-01 2
5730+
2025-01-01 3
5731+
Freq: AS-JAN, dtype: int64
5732+
5733+
Using `freq` which is the offset that the Timestamps will have
5734+
5735+
>>> s2 = pd.Series([1, 2, 3], index=idx)
5736+
>>> s2 = s2.to_timestamp(freq='M')
5737+
>>> s2
5738+
2023-01-31 1
5739+
2024-01-31 2
5740+
2025-01-31 3
5741+
Freq: A-JAN, dtype: int64
57135742
"""
57145743
if not isinstance(self.index, PeriodIndex):
57155744
raise TypeError(f"unsupported Type {type(self.index).__name__}")

pandas/tests/scalar/test_nat.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pytz
1010

1111
from pandas._libs.tslibs import iNaT
12+
from pandas.compat import is_numpy_dev
1213

1314
from pandas.core.dtypes.common import is_datetime64_any_dtype
1415

@@ -524,27 +525,24 @@ def test_to_numpy_alias():
524525
[
525526
Timedelta(0),
526527
Timedelta(0).to_pytimedelta(),
527-
pytest.param(
528-
Timedelta(0).to_timedelta64(),
529-
marks=pytest.mark.xfail(
530-
reason="td64 doesn't return NotImplemented, see numpy#17017"
531-
),
532-
),
528+
Timedelta(0).to_timedelta64(),
533529
Timestamp(0),
534530
Timestamp(0).to_pydatetime(),
535-
pytest.param(
536-
Timestamp(0).to_datetime64(),
537-
marks=pytest.mark.xfail(
538-
reason="dt64 doesn't return NotImplemented, see numpy#17017"
539-
),
540-
),
531+
Timestamp(0).to_datetime64(),
541532
Timestamp(0).tz_localize("UTC"),
542533
NaT,
543534
],
544535
)
545-
def test_nat_comparisons(compare_operators_no_eq_ne, other):
536+
def test_nat_comparisons(compare_operators_no_eq_ne, other, request):
546537
# GH 26039
547538
opname = compare_operators_no_eq_ne
539+
if isinstance(other, (np.datetime64, np.timedelta64)) and (
540+
opname in ["__eq__", "__ne__"] or not is_numpy_dev
541+
):
542+
mark = pytest.mark.xfail(
543+
reason="dt64/td64 don't return NotImplemented, see numpy#17017",
544+
)
545+
request.node.add_marker(mark)
548546

549547
assert getattr(NaT, opname)(other) is False
550548

0 commit comments

Comments
 (0)