From 5c296bb15dff7db660a4d09f87ef3eda88e76398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Tue, 20 Jun 2023 16:44:39 +0200 Subject: [PATCH 1/2] wip --- pandas/_libs/tslibs/nattype.pyx | 29 +++++++++++++++++++++++++++-- pandas/_libs/tslibs/timestamps.pyx | 28 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx index 75205a359db68..859bc92226da3 100644 --- a/pandas/_libs/tslibs/nattype.pyx +++ b/pandas/_libs/tslibs/nattype.pyx @@ -515,8 +515,6 @@ class NaTType(_NaT): dst = _make_error_func("dst", datetime) time = _make_error_func("time", datetime) toordinal = _make_error_func("toordinal", datetime) - tzname = _make_error_func("tzname", datetime) - utcoffset = _make_error_func("utcoffset", datetime) # "fromisocalendar" was introduced in 3.8 fromisocalendar = _make_error_func("fromisocalendar", datetime) @@ -524,7 +522,34 @@ class NaTType(_NaT): # ---------------------------------------------------------------------- # The remaining methods have docstrings copy/pasted from the analogous # Timestamp methods. + utcoffset = _make_error_func( + "utcoffset", + """ + Return utc offset. + Examples + -------- + >>> ts = pd.Timestamp('2023-01-01 10:00:00', tz='Europe/Brussels') + >>> ts + Timestamp('2023-01-01 10:00:00+0100', tz='Europe/Brussels') + >>> ts.utcoffset() + datetime.timedelta(seconds=3600) + """ + ) + tzname = _make_error_func( + "tzname", + """ + Return time zone name. + + Examples + -------- + >>> ts = pd.Timestamp('2023-01-01 10:00:00', tz='Europe/Brussels') + >>> ts + Timestamp('2023-01-01 10:00:00+0100', tz='Europe/Brussels') + >>> ts.tzname() + 'CET' + """ + ) ctime = _make_error_func( "ctime", """ diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index d66e856ef77cf..8c22ec9a24e54 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -1531,6 +1531,34 @@ class Timestamp(_Timestamp): ) from err return _dt.ctime() + def tzname(self): + """ + Return time zone name. + + Examples + -------- + >>> ts = pd.Timestamp('2023-01-01 10:00:00', tz='Europe/Brussels') + >>> ts + Timestamp('2023-01-01 10:00:00+0100', tz='Europe/Brussels') + >>> ts.tzname() + 'CET' + """ + return super().tzname() + + def utcoffset(self): + """ + Return utc offset. + + Examples + -------- + >>> ts = pd.Timestamp('2023-01-01 10:00:00', tz='Europe/Brussels') + >>> ts + Timestamp('2023-01-01 10:00:00+0100', tz='Europe/Brussels') + >>> ts.utcoffset() + datetime.timedelta(seconds=3600) + """ + return super().utcoffset() + # Issue 25016. @classmethod def strptime(cls, date_string, format): From a4584cab17388f9b0e6b0ec337f337155c1b28ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Tue, 20 Jun 2023 18:32:23 +0200 Subject: [PATCH 2/2] Examples for tzname, utcoffset, utctimetuple, weekday, strptime --- ci/code_checks.sh | 5 ----- pandas/_libs/tslibs/nattype.pyx | 30 +++++++++++++++++++++++++++++- pandas/_libs/tslibs/timestamps.pyx | 29 +++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index f63cc1fcc5767..f7d9614d7cfce 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -109,16 +109,11 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.Timestamp.dst \ pandas.Timestamp.isocalendar \ pandas.Timestamp.isoweekday \ - pandas.Timestamp.strptime \ pandas.Timestamp.time \ pandas.Timestamp.timetuple \ pandas.Timestamp.timetz \ pandas.Timestamp.to_datetime64 \ pandas.Timestamp.toordinal \ - pandas.Timestamp.tzname \ - pandas.Timestamp.utcoffset \ - pandas.Timestamp.utctimetuple \ - pandas.Timestamp.weekday \ pandas.arrays.TimedeltaArray \ pandas.Period.asfreq \ pandas.Period.now \ diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx index 859bc92226da3..6d51254518c20 100644 --- a/pandas/_libs/tslibs/nattype.pyx +++ b/pandas/_libs/tslibs/nattype.pyx @@ -429,6 +429,14 @@ class NaTType(_NaT): Return the day of the week represented by the date. Monday == 0 ... Sunday == 6. + + Examples + -------- + >>> ts = pd.Timestamp('2023-01-01') + >>> ts + Timestamp('2023-01-01 00:00:00') + >>> ts.weekday() + 6 """, ) isoweekday = _make_nan_func( @@ -508,7 +516,6 @@ class NaTType(_NaT): # _nat_methods date = _make_nat_func("date", datetime.date.__doc__) - utctimetuple = _make_error_func("utctimetuple", datetime) timetz = _make_error_func("timetz", datetime) timetuple = _make_error_func("timetuple", datetime) isocalendar = _make_error_func("isocalendar", datetime) @@ -522,6 +529,21 @@ class NaTType(_NaT): # ---------------------------------------------------------------------- # The remaining methods have docstrings copy/pasted from the analogous # Timestamp methods. + utctimetuple = _make_error_func( + "utctimetuple", + """ + Return UTC time tuple, compatible with time.localtime(). + + Examples + -------- + >>> ts = pd.Timestamp('2023-01-01 10:00:00', tz='Europe/Brussels') + >>> ts + Timestamp('2023-01-01 10:00:00+0100', tz='Europe/Brussels') + >>> ts.utctimetuple() + time.struct_time(tm_year=2023, tm_mon=1, tm_mday=1, tm_hour=9, + tm_min=0, tm_sec=0, tm_wday=6, tm_yday=1, tm_isdst=0) + """ + ) utcoffset = _make_error_func( "utcoffset", """ @@ -591,6 +613,12 @@ class NaTType(_NaT): Timestamp.strptime(string, format) Function is not implemented. Use pd.to_datetime(). + + Examples + -------- + >>> pd.Timestamp.strptime("2023-01-01", "%d/%m/%y") + Traceback (most recent call last): + NotImplementedError """, ) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 8c22ec9a24e54..b89a5f32af9d5 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -1559,6 +1559,21 @@ class Timestamp(_Timestamp): """ return super().utcoffset() + def utctimetuple(self): + """ + Return UTC time tuple, compatible with time.localtime(). + + Examples + -------- + >>> ts = pd.Timestamp('2023-01-01 10:00:00', tz='Europe/Brussels') + >>> ts + Timestamp('2023-01-01 10:00:00+0100', tz='Europe/Brussels') + >>> ts.utctimetuple() + time.struct_time(tm_year=2023, tm_mon=1, tm_mday=1, tm_hour=9, + tm_min=0, tm_sec=0, tm_wday=6, tm_yday=1, tm_isdst=0) + """ + return super().utctimetuple() + # Issue 25016. @classmethod def strptime(cls, date_string, format): @@ -1566,6 +1581,12 @@ class Timestamp(_Timestamp): Timestamp.strptime(string, format) Function is not implemented. Use pd.to_datetime(). + + Examples + -------- + >>> pd.Timestamp.strptime("2023-01-01", "%d/%m/%y") + Traceback (most recent call last): + NotImplementedError """ raise NotImplementedError( "Timestamp.strptime() is not implemented. " @@ -2422,6 +2443,14 @@ default 'raise' Return the day of the week represented by the date. Monday == 0 ... Sunday == 6. + + Examples + -------- + >>> ts = pd.Timestamp('2023-01-01') + >>> ts + Timestamp('2023-01-01 00:00:00') + >>> ts.weekday() + 6 """ # same as super().weekday(), but that breaks because of how # we have overridden year, see note in create_timestamp_from_ts