diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 3ddb9d4ff6d54..adda422296396 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -105,8 +105,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.errors.UnsupportedFunctionCall \ pandas.test \ pandas.NaT \ - pandas.Timestamp.as_unit \ - pandas.Timestamp.ctime \ pandas.Timestamp.date \ pandas.Timestamp.dst \ pandas.Timestamp.isocalendar \ diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx index ff07f5d799339..ea859a5f7d53d 100644 --- a/pandas/_libs/tslibs/nattype.pyx +++ b/pandas/_libs/tslibs/nattype.pyx @@ -501,7 +501,6 @@ class NaTType(_NaT): timetuple = _make_error_func("timetuple", datetime) isocalendar = _make_error_func("isocalendar", datetime) dst = _make_error_func("dst", datetime) - ctime = _make_error_func("ctime", datetime) time = _make_error_func("time", datetime) toordinal = _make_error_func("toordinal", datetime) tzname = _make_error_func("tzname", datetime) @@ -514,6 +513,21 @@ class NaTType(_NaT): # The remaining methods have docstrings copy/pasted from the analogous # Timestamp methods. + ctime = _make_error_func( + "ctime", + """ + Return ctime() style string. + + Examples + -------- + >>> ts = pd.Timestamp('2023-01-01 10:00:00.00') + >>> ts + Timestamp('2023-01-01 10:00:00') + >>> ts.ctime() + 'Sun Jan 1 10:00:00 2023' + """, + ) + strftime = _make_error_func( "strftime", """ @@ -1210,6 +1224,19 @@ default 'raise' Returns ------- Timestamp + + Examples + -------- + >>> ts = pd.Timestamp('2023-01-01 00:00:00.01') + >>> ts + Timestamp('2023-01-01 00:00:00.010000') + >>> ts.unit + 'ms' + >>> ts = ts.as_unit('s') + >>> ts + Timestamp('2023-01-01 00:00:00') + >>> ts.unit + 's' """ return c_NaT diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 4f09c0953f3d2..d66e856ef77cf 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -1122,6 +1122,19 @@ cdef class _Timestamp(ABCTimestamp): Returns ------- Timestamp + + Examples + -------- + >>> ts = pd.Timestamp('2023-01-01 00:00:00.01') + >>> ts + Timestamp('2023-01-01 00:00:00.010000') + >>> ts.unit + 'ms' + >>> ts = ts.as_unit('s') + >>> ts + Timestamp('2023-01-01 00:00:00') + >>> ts.unit + 's' """ dtype = np.dtype(f"M8[{unit}]") reso = get_unit_from_dtype(dtype) @@ -1493,6 +1506,31 @@ class Timestamp(_Timestamp): ) from err return _dt.strftime(format) + def ctime(self): + """ + Return ctime() style string. + + Examples + -------- + >>> ts = pd.Timestamp('2023-01-01 10:00:00.00') + >>> ts + Timestamp('2023-01-01 10:00:00') + >>> ts.ctime() + 'Sun Jan 1 10:00:00 2023' + """ + try: + _dt = datetime(self.year, self.month, self.day, + self.hour, self.minute, self.second, + self.microsecond, self.tzinfo, fold=self.fold) + except ValueError as err: + raise NotImplementedError( + "ctime not yet supported on Timestamps which " + "are outside the range of Python's standard library. " + "For now, please call the components you need (such as `.year` " + "and `.month`) and construct your string from there." + ) from err + return _dt.ctime() + # Issue 25016. @classmethod def strptime(cls, date_string, format): diff --git a/pandas/tests/scalar/test_nat.py b/pandas/tests/scalar/test_nat.py index 8296201345d2f..6f163b7ecd89d 100644 --- a/pandas/tests/scalar/test_nat.py +++ b/pandas/tests/scalar/test_nat.py @@ -491,7 +491,7 @@ def test_nat_arithmetic_ndarray(dtype, op, out_dtype): def test_nat_pinned_docstrings(): # see gh-17327 - assert NaT.ctime.__doc__ == datetime.ctime.__doc__ + assert NaT.ctime.__doc__ == Timestamp.ctime.__doc__ def test_to_numpy_alias():