Skip to content

Commit 734841f

Browse files
authored
DOC: Fixing EX01 - Added examples (#53619)
* ctime docstring * DOC Timestamp.as_unit and ctime
1 parent c65c8dd commit 734841f

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

ci/code_checks.sh

-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
105105
pandas.errors.UnsupportedFunctionCall \
106106
pandas.test \
107107
pandas.NaT \
108-
pandas.Timestamp.as_unit \
109-
pandas.Timestamp.ctime \
110108
pandas.Timestamp.date \
111109
pandas.Timestamp.dst \
112110
pandas.Timestamp.isocalendar \

pandas/_libs/tslibs/nattype.pyx

+28-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,6 @@ class NaTType(_NaT):
501501
timetuple = _make_error_func("timetuple", datetime)
502502
isocalendar = _make_error_func("isocalendar", datetime)
503503
dst = _make_error_func("dst", datetime)
504-
ctime = _make_error_func("ctime", datetime)
505504
time = _make_error_func("time", datetime)
506505
toordinal = _make_error_func("toordinal", datetime)
507506
tzname = _make_error_func("tzname", datetime)
@@ -514,6 +513,21 @@ class NaTType(_NaT):
514513
# The remaining methods have docstrings copy/pasted from the analogous
515514
# Timestamp methods.
516515

516+
ctime = _make_error_func(
517+
"ctime",
518+
"""
519+
Return ctime() style string.
520+
521+
Examples
522+
--------
523+
>>> ts = pd.Timestamp('2023-01-01 10:00:00.00')
524+
>>> ts
525+
Timestamp('2023-01-01 10:00:00')
526+
>>> ts.ctime()
527+
'Sun Jan 1 10:00:00 2023'
528+
""",
529+
)
530+
517531
strftime = _make_error_func(
518532
"strftime",
519533
"""
@@ -1210,6 +1224,19 @@ default 'raise'
12101224
Returns
12111225
-------
12121226
Timestamp
1227+
1228+
Examples
1229+
--------
1230+
>>> ts = pd.Timestamp('2023-01-01 00:00:00.01')
1231+
>>> ts
1232+
Timestamp('2023-01-01 00:00:00.010000')
1233+
>>> ts.unit
1234+
'ms'
1235+
>>> ts = ts.as_unit('s')
1236+
>>> ts
1237+
Timestamp('2023-01-01 00:00:00')
1238+
>>> ts.unit
1239+
's'
12131240
"""
12141241
return c_NaT
12151242

pandas/_libs/tslibs/timestamps.pyx

+38
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,19 @@ cdef class _Timestamp(ABCTimestamp):
11221122
Returns
11231123
-------
11241124
Timestamp
1125+
1126+
Examples
1127+
--------
1128+
>>> ts = pd.Timestamp('2023-01-01 00:00:00.01')
1129+
>>> ts
1130+
Timestamp('2023-01-01 00:00:00.010000')
1131+
>>> ts.unit
1132+
'ms'
1133+
>>> ts = ts.as_unit('s')
1134+
>>> ts
1135+
Timestamp('2023-01-01 00:00:00')
1136+
>>> ts.unit
1137+
's'
11251138
"""
11261139
dtype = np.dtype(f"M8[{unit}]")
11271140
reso = get_unit_from_dtype(dtype)
@@ -1493,6 +1506,31 @@ class Timestamp(_Timestamp):
14931506
) from err
14941507
return _dt.strftime(format)
14951508

1509+
def ctime(self):
1510+
"""
1511+
Return ctime() style string.
1512+
1513+
Examples
1514+
--------
1515+
>>> ts = pd.Timestamp('2023-01-01 10:00:00.00')
1516+
>>> ts
1517+
Timestamp('2023-01-01 10:00:00')
1518+
>>> ts.ctime()
1519+
'Sun Jan 1 10:00:00 2023'
1520+
"""
1521+
try:
1522+
_dt = datetime(self.year, self.month, self.day,
1523+
self.hour, self.minute, self.second,
1524+
self.microsecond, self.tzinfo, fold=self.fold)
1525+
except ValueError as err:
1526+
raise NotImplementedError(
1527+
"ctime not yet supported on Timestamps which "
1528+
"are outside the range of Python's standard library. "
1529+
"For now, please call the components you need (such as `.year` "
1530+
"and `.month`) and construct your string from there."
1531+
) from err
1532+
return _dt.ctime()
1533+
14961534
# Issue 25016.
14971535
@classmethod
14981536
def strptime(cls, date_string, format):

pandas/tests/scalar/test_nat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ def test_nat_arithmetic_ndarray(dtype, op, out_dtype):
491491

492492
def test_nat_pinned_docstrings():
493493
# see gh-17327
494-
assert NaT.ctime.__doc__ == datetime.ctime.__doc__
494+
assert NaT.ctime.__doc__ == Timestamp.ctime.__doc__
495495

496496

497497
def test_to_numpy_alias():

0 commit comments

Comments
 (0)