From 85f81938c340849506d9e4b12ab6fca0896450e4 Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Sat, 14 Nov 2020 18:48:56 +0000 Subject: [PATCH 01/20] SS01 errors fixed --- pandas/_libs/lib.pyx | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 0b0334d52c1e9..7e3c4b7f524ba 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -118,6 +118,8 @@ def memory_usage_of_objects(arr: object[:]) -> int64_t: def is_scalar(val: object) -> bool: """ + Check wether the provided object is of scalar type. + Parameters ---------- val : object @@ -927,6 +929,8 @@ def indices_fast(ndarray index, const int64_t[:] labels, list keys, def is_float(obj: object) -> bool: """ + Check wether the provided object is of float type. + Returns ------- bool @@ -936,6 +940,8 @@ def is_float(obj: object) -> bool: def is_integer(obj: object) -> bool: """ + Check wether the provided object is of integer type. + Returns ------- bool @@ -945,6 +951,8 @@ def is_integer(obj: object) -> bool: def is_bool(obj: object) -> bool: """ + Check wether the provided object is of boolean type. + Returns ------- bool @@ -954,6 +962,8 @@ def is_bool(obj: object) -> bool: def is_complex(obj: object) -> bool: """ + Check wether the provided object is of complex type. + Returns ------- bool From f459a0bb714979894dc3643b2c5a400276513fb9 Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Sat, 14 Nov 2020 18:57:05 +0000 Subject: [PATCH 02/20] trailing-whitespace error fixed --- pandas/_libs/lib.pyx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 7e3c4b7f524ba..c3f37ac08bb96 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -118,7 +118,7 @@ def memory_usage_of_objects(arr: object[:]) -> int64_t: def is_scalar(val: object) -> bool: """ - Check wether the provided object is of scalar type. + Return True if given object is scalar. Parameters ---------- @@ -929,7 +929,7 @@ def indices_fast(ndarray index, const int64_t[:] labels, list keys, def is_float(obj: object) -> bool: """ - Check wether the provided object is of float type. + Return a boolean if this is a float object. Returns ------- @@ -940,7 +940,7 @@ def is_float(obj: object) -> bool: def is_integer(obj: object) -> bool: """ - Check wether the provided object is of integer type. + Return a boolean if this is a integer object. Returns ------- @@ -951,7 +951,7 @@ def is_integer(obj: object) -> bool: def is_bool(obj: object) -> bool: """ - Check wether the provided object is of boolean type. + Return a boolean if this is a boolean object. Returns ------- @@ -962,7 +962,7 @@ def is_bool(obj: object) -> bool: def is_complex(obj: object) -> bool: """ - Check wether the provided object is of complex type. + Return a boolean if this is a complex object. Returns ------- From b6a0da28aa3e25060417dd2330e132af909c672f Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Sat, 14 Nov 2020 19:23:49 +0000 Subject: [PATCH 03/20] added coded to code_checks.sh script --- ci/code_checks.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index b5a6e32caa8e0..3eeee61f62a7e 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -225,7 +225,7 @@ fi ### DOCSTRINGS ### if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then - MSG='Validate docstrings (GL03, GL04, GL05, GL06, GL07, GL09, GL10, SS02, SS04, SS05, PR03, PR04, PR05, PR10, EX04, RT01, RT04, RT05, SA02, SA03)' ; echo $MSG + MSG='Validate docstrings (GL03, GL04, GL05, GL06, GL07, GL09, GL10, SS01, SS02, SS04, SS05, PR03, PR04, PR05, PR10, EX04, RT01, RT04, RT05, SA02, SA03)' ; echo $MSG $BASE_DIR/scripts/validate_docstrings.py --format=actions --errors=GL03,GL04,GL05,GL06,GL07,GL09,GL10,SS02,SS04,SS05,PR03,PR04,PR05,PR10,EX04,RT01,RT04,RT05,SA02,SA03 RET=$(($RET + $?)) ; echo $MSG "DONE" From 90abe5d7e06e5f7ef140e5fde911d39d95eaacda Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Sat, 14 Nov 2020 19:31:41 +0000 Subject: [PATCH 04/20] fixed docstrings consistency --- pandas/_libs/lib.pyx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index c3f37ac08bb96..f6d2d6e63340f 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -929,7 +929,7 @@ def indices_fast(ndarray index, const int64_t[:] labels, list keys, def is_float(obj: object) -> bool: """ - Return a boolean if this is a float object. + Return True if given object is float. Returns ------- @@ -940,7 +940,7 @@ def is_float(obj: object) -> bool: def is_integer(obj: object) -> bool: """ - Return a boolean if this is a integer object. + Return True if given object is integer. Returns ------- @@ -951,7 +951,7 @@ def is_integer(obj: object) -> bool: def is_bool(obj: object) -> bool: """ - Return a boolean if this is a boolean object. + Return True if given object is boolean. Returns ------- @@ -962,7 +962,7 @@ def is_bool(obj: object) -> bool: def is_complex(obj: object) -> bool: """ - Return a boolean if this is a complex object. + Return True if given object is complex. Returns ------- @@ -981,7 +981,7 @@ cpdef bint is_interval(object obj): def is_period(val: object) -> bool: """ - Return a boolean if this is a Period object. + Return True if given object is Period. Returns ------- From e99135beba3cdea70ee44eb55dc2da195ff3e811 Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Sat, 14 Nov 2020 19:36:35 +0000 Subject: [PATCH 05/20] mistaken file --- path_to_file.zip | Bin 5183 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 path_to_file.zip diff --git a/path_to_file.zip b/path_to_file.zip deleted file mode 100644 index 700a961e05f4604c5c2d1e7c3c8f93e02cf0edb4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5183 zcmaJ_1z1#V79K!iNI^gvB!w9o3F&Saq~S*-hGA$1kVaa%1f?4RDFGdk4h3mMq#Goq zk&Yeoch`0Q^~`hcooDWM&YkZ)anE}osu;aCH!%ezbzyK^@HO zp9UWYu4z8v;o=#8~6D|*XyRs+U4s8tA}h0*disK+LNzhLnu0KQ(Wg)e$vE^e4jcAKcc6zOm;r?JDZ?LC2DSd&QiEJJu7)L_? zMI4pGu1l3)FFF?y$#~a{zx|B2M2aQB%VL%N=c%O$tSfL_V(4(>0vEP< z=!+__XgGF^^Velq_j<2b^jh@20~&mA2^A@3ci-c_=I#r)GZ6Ik= zE!UGy|E1t!WS`V{Elbfk@8BIf2V=+8_zkmM2AI)V*KJER+VB9+p{?{#{iPfZ$iT9R z-bme1g!6}CNW*!ZBOd&ZkLw`)OR9^0gTz~fzMz4e8G68eZq&%&G!2*_e-#oZC> z(XB=a@@#{s616Q_I7PUbiH4r>T68|GLtQh6G>xsbd@D)3y=u^04>kgR?^KvVnOBe^ zN<2B#DRtQ;>XH&N&HMTJ;B_HgG#eo-uaJ%P+Uot)jYBC7y7XpKdZI}Z<-pX@IP03% z6#WGsNXWJ;d-gN<5+1H$RdjpHQ*9v)TCtcm>WEIqJO6sI7zl z2oA*$aIRo+v4%ojUAVtrc(3q@)>79*#Zto_h;hd7?f^jDo-H39h(R*HzWG{%nlrrQ zoyGQ+vZ%exeCtMo*y-^YyT0jkv1UXkqUeDHNJ9`K zTxe+@4IQ5RDvl|VDu=i&;D#LI(7JHA)+be{U7NU8UF=`B@p$@7Ckpa#8kkmFL1=Yb>qSSKP7-)$I?*I( zJWCzSu?hFq?;e(}EX{A-5V^i!f7(iIbXw%kzIhfDM<-9Ho%_5)GH_#jLgR&$)ee5{ zJjre^UjW%w;>>_eo@fRr<2juCuIz}O`IcWOI00Umo88R>f0IZ{CyEZ0aut;uT?5G) zuud9-&+KlB<0XEAK0G+G776p;i(?Rl4T}=W0jz(F?yJ`rD)n7lJ?)?_S0P>SM9X%P z2ft(HkUeTa3T3OKzbuVd6BJ)IWLzt4^hn_P~86KP(>tS|{cZkxA72_Ka`PdhXjbsX!nF$dhrc z>88A$#;i{Lp*VxL5q&!2bZB{)FQC9e1%Vv37bFVN#)6t4>DVLMiIgVafrp@HpuVoE zsw8<6!RZr*u${O-1L-deieez!m=@+r-M&%Y?mA{Q+Fs|rY$9baA_N|uZdqzVHQI?% ztq5b7%@i_65?S`pRi4k0_P<@?^#Eg(@DWvkcD^d)eYC zqeytYg)H@FPaVNqGyGOTl>`Q-sjT~j4Q8fB0YMX@ULTVdwF*BqrA-dlS*cAkU)h{~L6}Z8zcWZg)d*D?ULP6_}PB z#-e}9X3byI!Ls&7#5*-l?^H2$=)i34!8zTx@99KUPt_TV@}vdEpVH~7%RqV3)f#FK z{c-+YqBacGV<+S&8uvg7jc;OZi1wDJmI;pKhio8Ard}0wgo{@eQhJxBbUL`mZV&^3 zTGwwfJJ4K*6erarnpQ)u$*-Btep5`?v8w7tMgm@oFlX-U`8_(KU0!>Q%`2-Yr_}WX z`Q@*2!!TwEB5M+4nP|{O!PAiZR%L%mO{Ixc61d64$t=gT4skdTeOJz59!bWYrh zH>2+E7GnWUQack{V5$<22Q+sy3tByF(6b%qeI6b{LW40fkw^RuNM#8wE2G?_3#yRo zv}=RS5A?IFk%|aXh>pdkcX8cV!Kp@KBGCQB7>E)>D4yavyec!gsQg-1a*!TYO!kV( zMDjAxl|`DTeA$(erVp6WSmz9d^)=n!Hf{gr)!@*dve%6^df0Lg23$Wd)_}EZV~{=T zuCKdOv81;{b=gkhm-lJagPP38BEbFGcA3$qsdD}8P4q3!^_j8FhiPQ>Z<>XkZJ&dv z)!#()OB3gHdH^%tyU?YGSiT!0V5-O59tNY4-!|?p50ua#JCtA6rW zGE23W|Qynkt+ZFwWArov;7g%MjnhLLZ6+_PiC^j zJ$Jsr9{F630%piPirTORSV^WBJ68NV9;8VFC?oc?@EO%Wq{8U=XrtO%m>#wfDZr6i zy312!0|2$=^(UufCPYlUT0?-z=y_bHSeN0sR_i_oR#hQbLa^1GdF}!sj|5-mW+tw^ zV}cB;_Kj`_IS!jm{xCs8Wf{`0%^lubSXJJMdS|tYzAY^D?5?r7gwGY8;jcha!W{-E z=K1ch3@7%#N=K{*#=HPG6o`HEco%dJZIU-G!}9juG$J#S+s=N24-7lw-X)fs6|b7*8E z%wP2ht7R%U8Ag=M$^`7$GS{3WzgXdEWe;;k0CT{Gk4XytYE^DF>J*jK zxW^+gvu6d9lCpT+5oBJMlSQy>x2t>GO*%TNrM8tye_A718!(c%&#rQS5UI+qmFdV%(mw<*F1)gjevlB@EsW{MXDgK)qCByy3-7XxJbu-(^vcx+ zL{J{46rv+)pn$3CXM3>5Y`9{2IN3vV)51T8x~xz>@5@=F(P*Kpssht|37Kdl)H7C- zl=!ihQ&gxOBO%Yq2Kg8n1lS*SVs|TBW{Pj;MUtsr%c{Nr)a}7jg_^I*oSX-kVojOr zaTNmJzLM28<*-NN3B31qSMTJz**>fgM_=>+|2hR!WE@!oA%4u2s4%pJM?r%y%)S{| z^+87l>^+mdsuDx)TQaL9@RsKuwt!rqOrdr>gQaQG!^Y4Q?UAeseg$Y&N(t!Gyt`5i z4z=ayiJq@-AOh>G1x$#^)$Fn7YgD1 z2oKZ6?P8I-%GNPhq_+#q8_U!iRyTg1OUqdF!(vH+WaiPf#E0pCpuu2v!+LcufMW0F zS8j}13vQ)17g$&Q6UB*w&Pz-H;2!m#3O+Ha;9ELcKoE{jF5DK52ow#qx64TAwLx`I z!CSt#d3Bs1LKd8f0OaD3R1Ozs0}%cs%t%tnhPKI*!2S)(44JA3Y{8^^Ki`s_(oR}O; z%)1_He1Y840FC3$Q0cb_4oWY)C&SgkwTJg(+RN9g$|*{DC0k6_M-I(C>#VgE3uo7o zrX#Sy6#CDOYQrAerVy=r%tv3_8B+9}-+1+*l1PpOW58mvde=~_NdTnNziXIDl91pj zOYVAqfbmiys(!Tz@EQJL>K*@V^69~iq01|XLA?Cj%vbH{AG@RLzd~&0WX7chWdb6pYFfJU!2hdCW;lvfF;X^zS%krcvZUncc@EjS z!m1X}SYOAMTK}=U1AtSwt92kTt$rMC2*m38RWN3t9kOYVc#CY#Tgg7)>0$n zgG>D=4<}|_%s#>`k5Ok8tKBV@z&WcXcjLTU;Vq|*Vt>E%qvQU^*j$mvV9gn)<&Ml* zQ%6HT^ed;`uPyxKj&j=d?~M=|Iw=MzYTWW;;PUG%;Hnk+DgFUh-~M6z0#W^M1HV$` ztC7J^@eg=}dgLzygx?i^rIA-m`lt8@$~!Qc*5_hzt*h(#@`C%pnqsg ze>d=JMqZV+pW+{&Pxudo?sxTHW8*4qe~N!VEeeKz*U7(=7@~@a+7JK$M13SsUUK~Y i#Gf{Q=>G4#@83320r5Z6zaIwC0B8VF)XmNB82k&3gm)kS From 88131f807b5b379ab052e2d50eb16d4d67bdbc4f Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Sun, 15 Nov 2020 23:49:03 +0000 Subject: [PATCH 06/20] trailing-whitespace typo --- pandas/_libs/tslibs/timestamps.pyx | 108 +++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 242eb89d1e723..ca5758d26a6c2 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -436,6 +436,16 @@ cdef class _Timestamp(ABCTimestamp): def is_month_start(self) -> bool: """ Return True if date is first day of month. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14) + >>> ts.is_month_start + False + + >>> ts = pd.Timestamp(2020, 1, 1) + >>> ts.is_month_start + True """ if self.freq is None: # fast-path for non-business frequencies @@ -446,6 +456,16 @@ cdef class _Timestamp(ABCTimestamp): def is_month_end(self) -> bool: """ Return True if date is last day of month. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14) + >>> ts.is_month_end + False + + >>> ts = pd.Timestamp(2020, 12, 31) + >>> ts.is_month_end + True """ if self.freq is None: # fast-path for non-business frequencies @@ -456,6 +476,16 @@ cdef class _Timestamp(ABCTimestamp): def is_quarter_start(self) -> bool: """ Return True if date is first day of the quarter. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14) + >>> ts.is_month_end + False + + >>> ts = pd.Timestamp(2020, 4, 1) + >>> ts.is_quarter_start + True """ if self.freq is None: # fast-path for non-business frequencies @@ -466,6 +496,16 @@ cdef class _Timestamp(ABCTimestamp): def is_quarter_end(self) -> bool: """ Return True if date is last day of the quarter. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14) + >>> ts.is_quarter_end + False + + >>> ts = pd.Timestamp(2020, 3, 31) + >>> ts.is_quarter_end + True """ if self.freq is None: # fast-path for non-business frequencies @@ -476,6 +516,16 @@ cdef class _Timestamp(ABCTimestamp): def is_year_start(self) -> bool: """ Return True if date is first day of the year. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14) + >>> ts.is_year_start + False + + >>> ts = pd.Timestamp(2020, 1, 1) + >>> ts.is_year_start + True """ if self.freq is None: # fast-path for non-business frequencies @@ -486,6 +536,16 @@ cdef class _Timestamp(ABCTimestamp): def is_year_end(self) -> bool: """ Return True if date is last day of the year. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14) + >>> ts.is_year_end + False + + >>> ts = pd.Timestamp(2020, 12, 31) + >>> ts.is_year_end + True """ if self.freq is None: # fast-path for non-business frequencies @@ -536,6 +596,12 @@ cdef class _Timestamp(ABCTimestamp): def is_leap_year(self) -> bool: """ Return True if year is a leap year. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14) + >>> ts.is_leap_year + True """ return bool(ccalendar.is_leapyear(self.year)) @@ -543,6 +609,12 @@ cdef class _Timestamp(ABCTimestamp): def day_of_week(self) -> int: """ Return day of the week. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14) + >>> ts.day_of_week + 5 """ return self.weekday() @@ -550,6 +622,12 @@ cdef class _Timestamp(ABCTimestamp): def day_of_year(self) -> int: """ Return the day of the year. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14) + >>> ts.day_of_year + 74 """ return ccalendar.get_day_of_year(self.year, self.month, self.day) @@ -557,6 +635,12 @@ cdef class _Timestamp(ABCTimestamp): def quarter(self) -> int: """ Return the quarter of the year. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14) + >>> ts.quarter + 1 """ return ((self.month - 1) // 3) + 1 @@ -564,6 +648,12 @@ cdef class _Timestamp(ABCTimestamp): def week(self) -> int: """ Return the week number of the year. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14) + >>> ts.week + 11 """ return ccalendar.get_week_of_year(self.year, self.month, self.day) @@ -571,6 +661,12 @@ cdef class _Timestamp(ABCTimestamp): def days_in_month(self) -> int: """ Return the number of days in the month. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14) + >>> ts.days_in_month + 31 """ return ccalendar.get_days_in_month(self.year, self.month) @@ -691,6 +787,12 @@ cdef class _Timestamp(ABCTimestamp): def asm8(self) -> np.datetime64: """ Return numpy datetime64 format in nanoseconds. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14, 15) + >>> ts.asm8 + numpy.datetime64('2020-03-14T15:00:00.000000000') """ return np.datetime64(self.value, 'ns') @@ -1249,6 +1351,12 @@ timedelta}, default 'raise' def tz(self): """ Alias for tzinfo. + + Examples + -------- + >>> ts = pd.Timestamp(1584226800, unit='s', tz='Europe/Stockholm') + >>> ts.tz + """ return self.tzinfo From badea7bb4490a3bc425013b8918c446ae0bf19aa Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Mon, 16 Nov 2020 19:37:38 +0000 Subject: [PATCH 07/20] Example section for ceil method --- pandas/_libs/tslibs/timestamps.pyx | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index ca5758d26a6c2..52ecf8b925b45 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -1344,6 +1344,37 @@ timedelta}, default 'raise' Raises ------ ValueError if the freq cannot be converted. + + Example + ------- + Create a timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + + A timestamp can be ceiled to multiple frequencies: + + >>> ts.ceil(freq='H') # hour + Timestamp('2020-03-14 16:00:00') + >>> ts.ceil(freq='T') # minute + Timestamp('2020-03-14 15:33:00') + >>> ts.ceil(freq='S') # seconds + Timestamp('2020-03-14 15:32:53') + >>> ts.ceil(freq='L') # miliseconds + Timestamp('2020-03-14 15:32:52.193000') + >>> ts.ceil(freq='U') # microseconds + Timestamp('2020-03-14 15:32:52.192549') + >>> ts.ceil(freq='N') # nanoseconds + Timestamp('2020-03-14 15:32:52.192548651') + + ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): + + >>> ts.ceil('5T') # hour + Timestamp('2020-03-14 15:35:00') + + or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): + + >>> ts.ceil('1H30T') # hour + Timestamp('2020-03-14 16:30:00') """ return self._round(freq, RoundTo.PLUS_INFTY, ambiguous, nonexistent) From d49c16db4e1d772c3a1b8706e378fd683329059a Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Mon, 16 Nov 2020 19:59:51 +0000 Subject: [PATCH 08/20] Added example section for ceil, floor and round --- pandas/_libs/tslibs/timestamps.pyx | 68 ++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 52ecf8b925b45..cced5add5dfdf 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -1264,6 +1264,37 @@ timedelta}, default 'raise' Raises ------ ValueError if the freq cannot be converted + + Example + ------- + Create a timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + + A timestamp can be rounded using multiple frequency units: + + >>> ts.round(freq='H') # hour + Timestamp('2020-03-14 16:00:00') + >>> ts.round(freq='T') # minute + Timestamp('2020-03-14 15:33:00') + >>> ts.round(freq='S') # seconds + Timestamp('2020-03-14 15:32:52') + >>> ts.round(freq='L') # miliseconds + Timestamp('2020-03-14 15:32:52.193000') + >>> ts.round(freq='U') # microseconds + Timestamp('2020-03-14 15:32:52.192549') + >>> ts.round(freq='N') # nanoseconds + Timestamp('2020-03-14 15:32:52.192548651') + + ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): + + >>> ts.round(freq='5T') + Timestamp('2020-03-14 15:35:00') + + or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): + + >>> ts.round(freq='1H30T') + Timestamp('2020-03-14 15:00:00') """ return self._round( freq, RoundTo.NEAREST_HALF_EVEN, ambiguous, nonexistent @@ -1305,6 +1336,37 @@ timedelta}, default 'raise' Raises ------ ValueError if the freq cannot be converted. + + Example + ------- + Create a timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + + A timestamp can be floored using multiple frequency units: + + >>> ts.floor(freq='H') # hour + Timestamp('2020-03-14 15:00:00') + >>> ts.floor(freq='T') # minute + Timestamp('2020-03-14 15:32:00') + >>> ts.floor(freq='S') # seconds + Timestamp('2020-03-14 15:32:52') + >>> ts.floor(freq='L') # miliseconds + Timestamp('2020-03-14 15:32:52.192000') + >>> ts.floor(freq='U') # microseconds + Timestamp('2020-03-14 15:32:52.192548') + >>> ts.floor(freq='N') # nanoseconds + Timestamp('2020-03-14 15:32:52.192548651') + + ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): + + >>> ts.ceil(freq='5T') + Timestamp('2020-03-14 15:30:00') + + or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): + + >>> ts.ceil(freq='1H30T') + Timestamp('2020-03-14 15:00:00') """ return self._round(freq, RoundTo.MINUS_INFTY, ambiguous, nonexistent) @@ -1351,7 +1413,7 @@ timedelta}, default 'raise' >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') - A timestamp can be ceiled to multiple frequencies: + A timestamp can be ceiled using multiple frequency units: >>> ts.ceil(freq='H') # hour Timestamp('2020-03-14 16:00:00') @@ -1368,12 +1430,12 @@ timedelta}, default 'raise' ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): - >>> ts.ceil('5T') # hour + >>> ts.ceil(freq='5T') Timestamp('2020-03-14 15:35:00') or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): - >>> ts.ceil('1H30T') # hour + >>> ts.ceil(freq='1H30T') Timestamp('2020-03-14 16:30:00') """ return self._round(freq, RoundTo.PLUS_INFTY, ambiguous, nonexistent) From b5b26f39ac04dcad8c87e69d80ac69cc7a1c1dd4 Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Mon, 16 Nov 2020 21:56:42 +0000 Subject: [PATCH 09/20] example section added to methods day_name, month_name, fromordinal, utcfromtimestamp, fromtimestamp, combine. Typos in ceil, round, floor corrected --- pandas/_libs/tslibs/timestamps.pyx | 47 ++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index cced5add5dfdf..4ea89af34d801 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -574,6 +574,12 @@ cdef class _Timestamp(ABCTimestamp): Returns ------- str + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts.day_name() + 'Saturday' """ return self._get_date_name_field("day_name", locale) @@ -589,6 +595,12 @@ cdef class _Timestamp(ABCTimestamp): Returns ------- str + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts.month_name() + 'March' """ return self._get_date_name_field("month_name", locale) @@ -954,6 +966,11 @@ class Timestamp(_Timestamp): Offset to apply to the Timestamp. tz : str, pytz.timezone, dateutil.tz.tzfile or None Time zone for the Timestamp. + + Examples + -------- + >>> pd.Timestamp.fromordinal(737425) + Timestamp('2020-01-01 00:00:00') """ return cls(datetime.fromordinal(ordinal), freq=freq, tz=tz) @@ -1006,6 +1023,11 @@ class Timestamp(_Timestamp): Timestamp.utcfromtimestamp(ts) Construct a naive UTC datetime from a POSIX timestamp. + + Examples + -------- + >>> pd.Timestamp.fromtimestamp(1584199972) + Timestamp('2020-03-14 15:32:52') """ return cls(datetime.utcfromtimestamp(ts)) @@ -1015,6 +1037,13 @@ class Timestamp(_Timestamp): Timestamp.fromtimestamp(ts) Transform timestamp[, tz] to tz's local time from POSIX timestamp. + + Examples + -------- + >>> pd.Timestamp.utcfromtimestamp(1584199972) + Timestamp('2020-03-14 15:32:52') + + Note that the output may change depending on your local time. """ return cls(datetime.fromtimestamp(ts)) @@ -1053,6 +1082,12 @@ class Timestamp(_Timestamp): Timestamp.combine(date, time) Combine date, time into datetime with same date and time fields. + + Examples + -------- + >>> from datetime import date, time + >>> pd.Timestamp.combine(date(2020, 3, 14), time(15, 30, 15)) + Timestamp('2020-03-14 15:30:15') """ return cls(datetime.combine(date, time)) @@ -1265,8 +1300,8 @@ timedelta}, default 'raise' ------ ValueError if the freq cannot be converted - Example - ------- + Examples + -------- Create a timestamp object: >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') @@ -1337,8 +1372,8 @@ timedelta}, default 'raise' ------ ValueError if the freq cannot be converted. - Example - ------- + Examples + -------- Create a timestamp object: >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') @@ -1407,8 +1442,8 @@ timedelta}, default 'raise' ------ ValueError if the freq cannot be converted. - Example - ------- + Examples + -------- Create a timestamp object: >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') From 3793584a6f74ff7e9f54ea8382c1bad886d2532d Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Mon, 16 Nov 2020 22:53:19 +0000 Subject: [PATCH 10/20] trailing whitespace --- pandas/_libs/tslibs/timestamps.pyx | 84 ++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 4ea89af34d801..8dd585eb91568 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -819,6 +819,12 @@ cdef class _Timestamp(ABCTimestamp): Convert a Timestamp object to a native Python datetime object. If warn=True, issue a warning if nanoseconds is nonzero. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548') + >>> ts.to_pydatetime() + datetime.datetime(2020, 3, 14, 15, 32, 52, 192548) """ if self.nanosecond != 0 and warn: warnings.warn("Discarding nonzero nanoseconds in conversion", @@ -851,6 +857,12 @@ cdef class _Timestamp(ABCTimestamp): See Also -------- DatetimeIndex.to_numpy : Similar method for DatetimeIndex. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts.to_numpy() + numpy.datetime64('2020-03-14T15:32:52.192548651') """ return self.to_datetime64() @@ -987,6 +999,11 @@ class Timestamp(_Timestamp): ---------- tz : str or timezone object, default None Timezone to localize to. + + Examples + -------- + >>> pd.Timestamp.now() + Timestamp('2020-11-16 22:06:16.378782') """ if isinstance(tz, str): tz = maybe_get_tz(tz) @@ -1005,6 +1022,11 @@ class Timestamp(_Timestamp): ---------- tz : str or timezone object, default None Timezone to localize to. + + Examples + -------- + >>> pd.Timestamp.today() + Timestamp('2020-11-16 22:37:39.969883') """ return cls.now(tz) @@ -1014,6 +1036,11 @@ class Timestamp(_Timestamp): Timestamp.utcnow() Return a new Timestamp representing UTC day and time. + + Examples + -------- + >>> pd.Timestamp.utcnow() + Timestamp('2020-11-16 22:50:18.092888+0000', tz='UTC') """ return cls.now(UTC) @@ -1060,6 +1087,12 @@ class Timestamp(_Timestamp): Format string to convert Timestamp to string. See strftime documentation for more information on the format string: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts.strftime('%Y-%m-%d %X') + '2020-03-14 15:32:52' """ return datetime.strftime(self, format) @@ -1555,6 +1588,19 @@ default 'raise' ------ TypeError If the Timestamp is tz-aware and tz is not None. + + Examples + -------- + Create a naive timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts + Timestamp('2020-03-14 15:32:52.192548651') + + Add 'Europe/Stockholm' as timezone: + + >>> ts.tz_localize(tz='Europe/Stockholm') + Timestamp('2020-03-14 15:32:52.192548651+0100', tz='Europe/Stockholm') """ if ambiguous == 'infer': raise ValueError('Cannot infer offset with only one time.') @@ -1603,6 +1649,19 @@ default 'raise' ------ TypeError If Timestamp is tz-naive. + + Examples + -------- + Create a timestamp object with UTC timezone: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651', tz='UTC') + >>> ts + Timestamp('2020-03-14 15:32:52.192548651+0000', tz='UTC') + + Change to Tokyo timezone: + + >>> ts.tz_convert(tz='Asia/Tokyo') + Timestamp('2020-03-15 00:32:52.192548651+0900', tz='Asia/Tokyo') """ if self.tzinfo is None: # tz naive, use tz_localize @@ -1647,6 +1706,25 @@ default 'raise' Returns ------- Timestamp with fields replaced + + Examples + -------- + Create a timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651', tz='UTC') + >>> ts + Timestamp('2020-03-14 15:32:52.192548651+0000', tz='UTC') + + Replace year and the hour: + + >>> ts.replace(year=1999, hour=10) + Timestamp('1999-03-14 10:32:52.192548651+0000', tz='UTC') + + Replace timezone (not a conversion): + + >>> import pytz + >>> ts.replace(tzinfo=pytz.timezone('US/Pacific')) + Timestamp('2020-03-14 15:32:52.192548651-0700', tz='US/Pacific') """ cdef: @@ -1726,6 +1804,12 @@ default 'raise' """ Convert TimeStamp to a Julian Date. 0 Julian date is noon January 1, 4713 BC. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52') + >>> ts.to_julian_date() + 2458923.147824074 """ year = self.year month = self.month From 12e560c12f3cafe13dcb134f8085a2aebc7e7dbc Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Mon, 16 Nov 2020 23:48:29 +0000 Subject: [PATCH 11/20] typo in floor method --- pandas/_libs/tslibs/timestamps.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index f3e4ba8ac0e2b..7ca52bb8726d3 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -1428,12 +1428,12 @@ timedelta}, default 'raise' ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): - >>> ts.ceil(freq='5T') + >>> ts.floor(freq='5T') Timestamp('2020-03-14 15:30:00') or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): - >>> ts.ceil(freq='1H30T') + >>> ts.floor(freq='1H30T') Timestamp('2020-03-14 15:00:00') """ return self._round(freq, RoundTo.MINUS_INFTY, ambiguous, nonexistent) From 8897d309776198815f36dfb2f2562a03b80a9998 Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Tue, 17 Nov 2020 00:06:00 +0000 Subject: [PATCH 12/20] added example section for method normalize --- pandas/_libs/tslibs/timestamps.pyx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 7ca52bb8726d3..c5dff20ad5297 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -688,6 +688,12 @@ cdef class _Timestamp(ABCTimestamp): def normalize(self) -> "Timestamp": """ Normalize Timestamp to midnight, preserving tz information. + + Examples + -------- + >>> ts = pd.Timestamp(2020, 3, 14, 15, 30) + >>> ts.normalize() + Timestamp('2020-03-14 00:00:00') """ cdef: local_val = self._maybe_convert_value_to_local() From 45bafac2398941507030887f0f34366f61dc4747 Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Tue, 17 Nov 2020 00:17:29 +0000 Subject: [PATCH 13/20] typo --- pandas/_libs/tslibs/timestamps.pyx | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index c5dff20ad5297..e4b5f6b8aac22 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -815,7 +815,15 @@ cdef class _Timestamp(ABCTimestamp): return np.datetime64(self.value, 'ns') def timestamp(self): - """Return POSIX timestamp as float.""" + """ + Return POSIX timestamp as float. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548') + >>> ts.timestamp() + 1584199972.192548 + """ # GH 17329 # Note: Naive timestamps will not match datetime.stdlib return round(self.value / 1e9, 6) @@ -875,6 +883,18 @@ cdef class _Timestamp(ABCTimestamp): def to_period(self, freq=None): """ Return an period of which this timestamp is an observation. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts.to_period(freq='Y) # Year end frequency + numpy.datetime64('2020-03-14T15:32:52.192548651') + >>> ts.to_period(freq='M') # Month end frequency + Period('2020-03', 'M') + >>> ts.to_period(freq='W') # Weekly frequency + Period('2020-03-09/2020-03-15', 'W-SUN') + >>> ts.to_period(freq='Q') # Quarter end frequency + Period('2020Q1', 'Q-DEC') """ from pandas import Period From f6bb54e6a2091d8a330f7c62a47e6f9f2c576e66 Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Thu, 19 Nov 2020 22:51:04 +0000 Subject: [PATCH 14/20] recommendations from pull request review --- pandas/_libs/tslibs/timestamps.pyx | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index e4b5f6b8aac22..80c37a1f20b38 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -889,10 +889,13 @@ cdef class _Timestamp(ABCTimestamp): >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') >>> ts.to_period(freq='Y) # Year end frequency numpy.datetime64('2020-03-14T15:32:52.192548651') + >>> ts.to_period(freq='M') # Month end frequency Period('2020-03', 'M') + >>> ts.to_period(freq='W') # Weekly frequency Period('2020-03-09/2020-03-15', 'W-SUN') + >>> ts.to_period(freq='Q') # Quarter end frequency Period('2020Q1', 'Q-DEC') """ @@ -1369,16 +1372,15 @@ timedelta}, default 'raise' >>> ts.round(freq='H') # hour Timestamp('2020-03-14 16:00:00') + >>> ts.round(freq='T') # minute Timestamp('2020-03-14 15:33:00') + >>> ts.round(freq='S') # seconds Timestamp('2020-03-14 15:32:52') - >>> ts.round(freq='L') # miliseconds + + >>> ts.round(freq='L') # milliseconds Timestamp('2020-03-14 15:32:52.193000') - >>> ts.round(freq='U') # microseconds - Timestamp('2020-03-14 15:32:52.192549') - >>> ts.round(freq='N') # nanoseconds - Timestamp('2020-03-14 15:32:52.192548651') ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): @@ -1441,14 +1443,13 @@ timedelta}, default 'raise' >>> ts.floor(freq='H') # hour Timestamp('2020-03-14 15:00:00') + >>> ts.floor(freq='T') # minute Timestamp('2020-03-14 15:32:00') + >>> ts.floor(freq='S') # seconds Timestamp('2020-03-14 15:32:52') - >>> ts.floor(freq='L') # miliseconds - Timestamp('2020-03-14 15:32:52.192000') - >>> ts.floor(freq='U') # microseconds - Timestamp('2020-03-14 15:32:52.192548') + >>> ts.floor(freq='N') # nanoseconds Timestamp('2020-03-14 15:32:52.192548651') @@ -1511,16 +1512,15 @@ timedelta}, default 'raise' >>> ts.ceil(freq='H') # hour Timestamp('2020-03-14 16:00:00') + >>> ts.ceil(freq='T') # minute Timestamp('2020-03-14 15:33:00') + >>> ts.ceil(freq='S') # seconds Timestamp('2020-03-14 15:32:53') - >>> ts.ceil(freq='L') # miliseconds - Timestamp('2020-03-14 15:32:52.193000') + >>> ts.ceil(freq='U') # microseconds Timestamp('2020-03-14 15:32:52.192549') - >>> ts.ceil(freq='N') # nanoseconds - Timestamp('2020-03-14 15:32:52.192548651') ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): From 2f7a12430f82541e5cd15c002876e1292fba1e43 Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Fri, 20 Nov 2020 16:01:59 +0000 Subject: [PATCH 15/20] typo --- pandas/_libs/tslibs/nattype.pyx | 229 +++++++++++++++++++++++++++++++- 1 file changed, 228 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx index 561143f48e0ec..1cbc42ad99083 100644 --- a/pandas/_libs/tslibs/nattype.pyx +++ b/pandas/_libs/tslibs/nattype.pyx @@ -252,6 +252,12 @@ cdef class _NaT(datetime): See Also -------- DatetimeIndex.to_numpy : Similar method for DatetimeIndex. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts.to_numpy() + numpy.datetime64('2020-03-14T15:32:52.192548651') """ return self.to_datetime64() @@ -395,6 +401,12 @@ class NaTType(_NaT): Returns ------- str + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts.month_name() + 'March' """, ) day_name = _make_nan_func( @@ -410,6 +422,12 @@ class NaTType(_NaT): Returns ------- str + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts.day_name() + 'Saturday' """, ) # _nat_methods @@ -448,6 +466,12 @@ class NaTType(_NaT): Format string to convert Timestamp to string. See strftime documentation for more information on the format string: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts.strftime('%Y-%m-%d %X') + '2020-03-14 15:32:52' """, ) @@ -466,6 +490,11 @@ class NaTType(_NaT): Timestamp.utcfromtimestamp(ts) Construct a naive UTC datetime from a POSIX timestamp. + + Examples + -------- + >>> pd.Timestamp.fromtimestamp(1584199972) + Timestamp('2020-03-14 15:32:52') """, ) fromtimestamp = _make_error_func( @@ -474,6 +503,13 @@ class NaTType(_NaT): Timestamp.fromtimestamp(ts) Transform timestamp[, tz] to tz's local time from POSIX timestamp. + + Examples + -------- + >>> pd.Timestamp.utcfromtimestamp(1584199972) + Timestamp('2020-03-14 15:32:52') + + Note that the output may change depending on your local time. """, ) combine = _make_error_func( @@ -482,6 +518,12 @@ class NaTType(_NaT): Timestamp.combine(date, time) Combine date, time into datetime with same date and time fields. + + Examples + -------- + >>> from datetime import date, time + >>> pd.Timestamp.combine(date(2020, 3, 14), time(15, 30, 15)) + Timestamp('2020-03-14 15:30:15') """, ) utcnow = _make_error_func( @@ -490,10 +532,26 @@ class NaTType(_NaT): Timestamp.utcnow() Return a new Timestamp representing UTC day and time. + + Examples + -------- + >>> pd.Timestamp.utcnow() + Timestamp('2020-11-16 22:50:18.092888+0000', tz='UTC') """, ) - timestamp = _make_error_func("timestamp", "Return POSIX timestamp as float.") + timestamp = _make_error_func( + "timestamp", + """ + Return POSIX timestamp as float. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548') + >>> ts.timestamp() + 1584199972.192548 + """ + ) # GH9513 NaT methods (except to_datetime64) to raise, return np.nan, or # return NaT create functions that raise, for binding to NaTType @@ -516,6 +574,19 @@ class NaTType(_NaT): ------ TypeError If Timestamp is tz-naive. + + Examples + -------- + Create a timestamp object with UTC timezone: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651', tz='UTC') + >>> ts + Timestamp('2020-03-14 15:32:52.192548651+0000', tz='UTC') + + Change to Tokyo timezone: + + >>> ts.tz_convert(tz='Asia/Tokyo') + Timestamp('2020-03-15 00:32:52.192548651+0900', tz='Asia/Tokyo') """, ) fromordinal = _make_error_func( @@ -534,6 +605,11 @@ class NaTType(_NaT): Offset to apply to the Timestamp. tz : str, pytz.timezone, dateutil.tz.tzfile or None Time zone for the Timestamp. + + Examples + -------- + >>> pd.Timestamp.fromordinal(737425) + Timestamp('2020-01-01 00:00:00') """, ) @@ -544,6 +620,12 @@ class NaTType(_NaT): Convert a Timestamp object to a native Python datetime object. If warn=True, issue a warning if nanoseconds is nonzero. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548') + >>> ts.to_pydatetime() + datetime.datetime(2020, 3, 14, 15, 32, 52, 192548) """, ) @@ -559,6 +641,11 @@ class NaTType(_NaT): ---------- tz : str or timezone object, default None Timezone to localize to. + + Examples + -------- + >>> pd.Timestamp.now() + Timestamp('2020-11-16 22:06:16.378782') """, ) today = _make_nat_func( @@ -574,6 +661,11 @@ class NaTType(_NaT): ---------- tz : str or timezone object, default None Timezone to localize to. + + Examples + -------- + >>> pd.Timestamp.today() + Timestamp('2020-11-16 22:37:39.969883') """, ) round = _make_nat_func( @@ -617,6 +709,36 @@ timedelta}, default 'raise' Raises ------ ValueError if the freq cannot be converted + + Examples + -------- + Create a timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + + A timestamp can be rounded using multiple frequency units: + + >>> ts.round(freq='H') # hour + Timestamp('2020-03-14 16:00:00') + + >>> ts.round(freq='T') # minute + Timestamp('2020-03-14 15:33:00') + + >>> ts.round(freq='S') # seconds + Timestamp('2020-03-14 15:32:52') + + >>> ts.round(freq='L') # milliseconds + Timestamp('2020-03-14 15:32:52.193000') + + ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): + + >>> ts.round(freq='5T') + Timestamp('2020-03-14 15:35:00') + + or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): + + >>> ts.round(freq='1H30T') + Timestamp('2020-03-14 15:00:00') """, ) floor = _make_nat_func( @@ -656,6 +778,36 @@ timedelta}, default 'raise' Raises ------ ValueError if the freq cannot be converted. + + Examples + -------- + Create a timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + + A timestamp can be floored using multiple frequency units: + + >>> ts.floor(freq='H') # hour + Timestamp('2020-03-14 15:00:00') + + >>> ts.floor(freq='T') # minute + Timestamp('2020-03-14 15:32:00') + + >>> ts.floor(freq='S') # seconds + Timestamp('2020-03-14 15:32:52') + + >>> ts.floor(freq='N') # nanoseconds + Timestamp('2020-03-14 15:32:52.192548651') + + ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): + + >>> ts.floor(freq='5T') + Timestamp('2020-03-14 15:30:00') + + or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): + + >>> ts.floor(freq='1H30T') + Timestamp('2020-03-14 15:00:00') """, ) ceil = _make_nat_func( @@ -695,6 +847,36 @@ timedelta}, default 'raise' Raises ------ ValueError if the freq cannot be converted. + + Examples + -------- + Create a timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + + A timestamp can be ceiled using multiple frequency units: + + >>> ts.ceil(freq='H') # hour + Timestamp('2020-03-14 16:00:00') + + >>> ts.ceil(freq='T') # minute + Timestamp('2020-03-14 15:33:00') + + >>> ts.ceil(freq='S') # seconds + Timestamp('2020-03-14 15:32:53') + + >>> ts.ceil(freq='U') # microseconds + Timestamp('2020-03-14 15:32:52.192549') + + ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): + + >>> ts.ceil(freq='5T') + Timestamp('2020-03-14 15:35:00') + + or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): + + >>> ts.ceil(freq='1H30T') + Timestamp('2020-03-14 16:30:00') """, ) @@ -717,6 +899,19 @@ timedelta}, default 'raise' ------ TypeError If Timestamp is tz-naive. + + Examples + -------- + Create a timestamp object with UTC timezone: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651', tz='UTC') + >>> ts + Timestamp('2020-03-14 15:32:52.192548651+0000', tz='UTC') + + Change to Tokyo timezone: + + >>> ts.tz_convert(tz='Asia/Tokyo') + Timestamp('2020-03-15 00:32:52.192548651+0900', tz='Asia/Tokyo') """, ) tz_localize = _make_nat_func( @@ -772,6 +967,19 @@ default 'raise' ------ TypeError If the Timestamp is tz-aware and tz is not None. + + Examples + -------- + Create a naive timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts + Timestamp('2020-03-14 15:32:52.192548651') + + Add 'Europe/Stockholm' as timezone: + + >>> ts.tz_localize(tz='Europe/Stockholm') + Timestamp('2020-03-14 15:32:52.192548651+0100', tz='Europe/Stockholm') """, ) replace = _make_nat_func( @@ -795,6 +1003,25 @@ default 'raise' Returns ------- Timestamp with fields replaced + + Examples + -------- + Create a timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651', tz='UTC') + >>> ts + Timestamp('2020-03-14 15:32:52.192548651+0000', tz='UTC') + + Replace year and the hour: + + >>> ts.replace(year=1999, hour=10) + Timestamp('1999-03-14 10:32:52.192548651+0000', tz='UTC') + + Replace timezone (not a conversion): + + >>> import pytz + >>> ts.replace(tzinfo=pytz.timezone('US/Pacific')) + Timestamp('2020-03-14 15:32:52.192548651-0700', tz='US/Pacific') """, ) From eed4712a764e9ab56a36f054b7c113757055f7e2 Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Mon, 23 Nov 2020 17:10:51 +0000 Subject: [PATCH 16/20] revert changes. to be addressed in another PR --- pandas/_libs/tslibs/nattype.pyx | 229 +------------------------------- 1 file changed, 1 insertion(+), 228 deletions(-) diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx index 1cbc42ad99083..561143f48e0ec 100644 --- a/pandas/_libs/tslibs/nattype.pyx +++ b/pandas/_libs/tslibs/nattype.pyx @@ -252,12 +252,6 @@ cdef class _NaT(datetime): See Also -------- DatetimeIndex.to_numpy : Similar method for DatetimeIndex. - - Examples - -------- - >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') - >>> ts.to_numpy() - numpy.datetime64('2020-03-14T15:32:52.192548651') """ return self.to_datetime64() @@ -401,12 +395,6 @@ class NaTType(_NaT): Returns ------- str - - Examples - -------- - >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') - >>> ts.month_name() - 'March' """, ) day_name = _make_nan_func( @@ -422,12 +410,6 @@ class NaTType(_NaT): Returns ------- str - - Examples - -------- - >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') - >>> ts.day_name() - 'Saturday' """, ) # _nat_methods @@ -466,12 +448,6 @@ class NaTType(_NaT): Format string to convert Timestamp to string. See strftime documentation for more information on the format string: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior. - - Examples - -------- - >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') - >>> ts.strftime('%Y-%m-%d %X') - '2020-03-14 15:32:52' """, ) @@ -490,11 +466,6 @@ class NaTType(_NaT): Timestamp.utcfromtimestamp(ts) Construct a naive UTC datetime from a POSIX timestamp. - - Examples - -------- - >>> pd.Timestamp.fromtimestamp(1584199972) - Timestamp('2020-03-14 15:32:52') """, ) fromtimestamp = _make_error_func( @@ -503,13 +474,6 @@ class NaTType(_NaT): Timestamp.fromtimestamp(ts) Transform timestamp[, tz] to tz's local time from POSIX timestamp. - - Examples - -------- - >>> pd.Timestamp.utcfromtimestamp(1584199972) - Timestamp('2020-03-14 15:32:52') - - Note that the output may change depending on your local time. """, ) combine = _make_error_func( @@ -518,12 +482,6 @@ class NaTType(_NaT): Timestamp.combine(date, time) Combine date, time into datetime with same date and time fields. - - Examples - -------- - >>> from datetime import date, time - >>> pd.Timestamp.combine(date(2020, 3, 14), time(15, 30, 15)) - Timestamp('2020-03-14 15:30:15') """, ) utcnow = _make_error_func( @@ -532,26 +490,10 @@ class NaTType(_NaT): Timestamp.utcnow() Return a new Timestamp representing UTC day and time. - - Examples - -------- - >>> pd.Timestamp.utcnow() - Timestamp('2020-11-16 22:50:18.092888+0000', tz='UTC') """, ) - timestamp = _make_error_func( - "timestamp", - """ - Return POSIX timestamp as float. - - Examples - -------- - >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548') - >>> ts.timestamp() - 1584199972.192548 - """ - ) + timestamp = _make_error_func("timestamp", "Return POSIX timestamp as float.") # GH9513 NaT methods (except to_datetime64) to raise, return np.nan, or # return NaT create functions that raise, for binding to NaTType @@ -574,19 +516,6 @@ class NaTType(_NaT): ------ TypeError If Timestamp is tz-naive. - - Examples - -------- - Create a timestamp object with UTC timezone: - - >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651', tz='UTC') - >>> ts - Timestamp('2020-03-14 15:32:52.192548651+0000', tz='UTC') - - Change to Tokyo timezone: - - >>> ts.tz_convert(tz='Asia/Tokyo') - Timestamp('2020-03-15 00:32:52.192548651+0900', tz='Asia/Tokyo') """, ) fromordinal = _make_error_func( @@ -605,11 +534,6 @@ class NaTType(_NaT): Offset to apply to the Timestamp. tz : str, pytz.timezone, dateutil.tz.tzfile or None Time zone for the Timestamp. - - Examples - -------- - >>> pd.Timestamp.fromordinal(737425) - Timestamp('2020-01-01 00:00:00') """, ) @@ -620,12 +544,6 @@ class NaTType(_NaT): Convert a Timestamp object to a native Python datetime object. If warn=True, issue a warning if nanoseconds is nonzero. - - Examples - -------- - >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548') - >>> ts.to_pydatetime() - datetime.datetime(2020, 3, 14, 15, 32, 52, 192548) """, ) @@ -641,11 +559,6 @@ class NaTType(_NaT): ---------- tz : str or timezone object, default None Timezone to localize to. - - Examples - -------- - >>> pd.Timestamp.now() - Timestamp('2020-11-16 22:06:16.378782') """, ) today = _make_nat_func( @@ -661,11 +574,6 @@ class NaTType(_NaT): ---------- tz : str or timezone object, default None Timezone to localize to. - - Examples - -------- - >>> pd.Timestamp.today() - Timestamp('2020-11-16 22:37:39.969883') """, ) round = _make_nat_func( @@ -709,36 +617,6 @@ timedelta}, default 'raise' Raises ------ ValueError if the freq cannot be converted - - Examples - -------- - Create a timestamp object: - - >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') - - A timestamp can be rounded using multiple frequency units: - - >>> ts.round(freq='H') # hour - Timestamp('2020-03-14 16:00:00') - - >>> ts.round(freq='T') # minute - Timestamp('2020-03-14 15:33:00') - - >>> ts.round(freq='S') # seconds - Timestamp('2020-03-14 15:32:52') - - >>> ts.round(freq='L') # milliseconds - Timestamp('2020-03-14 15:32:52.193000') - - ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): - - >>> ts.round(freq='5T') - Timestamp('2020-03-14 15:35:00') - - or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): - - >>> ts.round(freq='1H30T') - Timestamp('2020-03-14 15:00:00') """, ) floor = _make_nat_func( @@ -778,36 +656,6 @@ timedelta}, default 'raise' Raises ------ ValueError if the freq cannot be converted. - - Examples - -------- - Create a timestamp object: - - >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') - - A timestamp can be floored using multiple frequency units: - - >>> ts.floor(freq='H') # hour - Timestamp('2020-03-14 15:00:00') - - >>> ts.floor(freq='T') # minute - Timestamp('2020-03-14 15:32:00') - - >>> ts.floor(freq='S') # seconds - Timestamp('2020-03-14 15:32:52') - - >>> ts.floor(freq='N') # nanoseconds - Timestamp('2020-03-14 15:32:52.192548651') - - ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): - - >>> ts.floor(freq='5T') - Timestamp('2020-03-14 15:30:00') - - or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): - - >>> ts.floor(freq='1H30T') - Timestamp('2020-03-14 15:00:00') """, ) ceil = _make_nat_func( @@ -847,36 +695,6 @@ timedelta}, default 'raise' Raises ------ ValueError if the freq cannot be converted. - - Examples - -------- - Create a timestamp object: - - >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') - - A timestamp can be ceiled using multiple frequency units: - - >>> ts.ceil(freq='H') # hour - Timestamp('2020-03-14 16:00:00') - - >>> ts.ceil(freq='T') # minute - Timestamp('2020-03-14 15:33:00') - - >>> ts.ceil(freq='S') # seconds - Timestamp('2020-03-14 15:32:53') - - >>> ts.ceil(freq='U') # microseconds - Timestamp('2020-03-14 15:32:52.192549') - - ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): - - >>> ts.ceil(freq='5T') - Timestamp('2020-03-14 15:35:00') - - or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): - - >>> ts.ceil(freq='1H30T') - Timestamp('2020-03-14 16:30:00') """, ) @@ -899,19 +717,6 @@ timedelta}, default 'raise' ------ TypeError If Timestamp is tz-naive. - - Examples - -------- - Create a timestamp object with UTC timezone: - - >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651', tz='UTC') - >>> ts - Timestamp('2020-03-14 15:32:52.192548651+0000', tz='UTC') - - Change to Tokyo timezone: - - >>> ts.tz_convert(tz='Asia/Tokyo') - Timestamp('2020-03-15 00:32:52.192548651+0900', tz='Asia/Tokyo') """, ) tz_localize = _make_nat_func( @@ -967,19 +772,6 @@ default 'raise' ------ TypeError If the Timestamp is tz-aware and tz is not None. - - Examples - -------- - Create a naive timestamp object: - - >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') - >>> ts - Timestamp('2020-03-14 15:32:52.192548651') - - Add 'Europe/Stockholm' as timezone: - - >>> ts.tz_localize(tz='Europe/Stockholm') - Timestamp('2020-03-14 15:32:52.192548651+0100', tz='Europe/Stockholm') """, ) replace = _make_nat_func( @@ -1003,25 +795,6 @@ default 'raise' Returns ------- Timestamp with fields replaced - - Examples - -------- - Create a timestamp object: - - >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651', tz='UTC') - >>> ts - Timestamp('2020-03-14 15:32:52.192548651+0000', tz='UTC') - - Replace year and the hour: - - >>> ts.replace(year=1999, hour=10) - Timestamp('1999-03-14 10:32:52.192548651+0000', tz='UTC') - - Replace timezone (not a conversion): - - >>> import pytz - >>> ts.replace(tzinfo=pytz.timezone('US/Pacific')) - Timestamp('2020-03-14 15:32:52.192548651-0700', tz='US/Pacific') """, ) From 1563e3942a1010b815441f7da15b21c2fc8ebc74 Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Thu, 26 Nov 2020 23:14:45 +0000 Subject: [PATCH 17/20] Added NaT examples in nattype and timestamp docstrings where needed. --- pandas/_libs/tslibs/nattype.pyx | 304 ++++++++++++++++++++++++++++- pandas/_libs/tslibs/timestamps.pyx | 67 ++++++- 2 files changed, 369 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx index 561143f48e0ec..a3a7fd97d3aef 100644 --- a/pandas/_libs/tslibs/nattype.pyx +++ b/pandas/_libs/tslibs/nattype.pyx @@ -252,6 +252,17 @@ cdef class _NaT(datetime): See Also -------- DatetimeIndex.to_numpy : Similar method for DatetimeIndex. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts.to_numpy() + numpy.datetime64('2020-03-14T15:32:52.192548651') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.to_numpy() + numpy.datetime64('NaT') """ return self.to_datetime64() @@ -395,6 +406,17 @@ class NaTType(_NaT): Returns ------- str + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts.month_name() + 'March' + + Analogous for ``pd.NaT``: + + >>> pd.NaT.month_name() + nan """, ) day_name = _make_nan_func( @@ -410,6 +432,17 @@ class NaTType(_NaT): Returns ------- str + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts.day_name() + 'Saturday' + + Analogous for ``pd.NaT``: + + >>> pd.NaT.day_name() + nan """, ) # _nat_methods @@ -448,6 +481,12 @@ class NaTType(_NaT): Format string to convert Timestamp to string. See strftime documentation for more information on the format string: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts.strftime('%Y-%m-%d %X') + '2020-03-14 15:32:52' """, ) @@ -466,6 +505,11 @@ class NaTType(_NaT): Timestamp.utcfromtimestamp(ts) Construct a naive UTC datetime from a POSIX timestamp. + + Examples + -------- + >>> pd.Timestamp.fromtimestamp(1584199972) + Timestamp('2020-03-14 15:32:52') """, ) fromtimestamp = _make_error_func( @@ -474,6 +518,13 @@ class NaTType(_NaT): Timestamp.fromtimestamp(ts) Transform timestamp[, tz] to tz's local time from POSIX timestamp. + + Examples + -------- + >>> pd.Timestamp.utcfromtimestamp(1584199972) + Timestamp('2020-03-14 15:32:52') + + Note that the output may change depending on your local time. """, ) combine = _make_error_func( @@ -482,6 +533,12 @@ class NaTType(_NaT): Timestamp.combine(date, time) Combine date, time into datetime with same date and time fields. + + Examples + -------- + >>> from datetime import date, time + >>> pd.Timestamp.combine(date(2020, 3, 14), time(15, 30, 15)) + Timestamp('2020-03-14 15:30:15') """, ) utcnow = _make_error_func( @@ -490,10 +547,26 @@ class NaTType(_NaT): Timestamp.utcnow() Return a new Timestamp representing UTC day and time. + + Examples + -------- + >>> pd.Timestamp.utcnow() + Timestamp('2020-11-16 22:50:18.092888+0000', tz='UTC') """, ) - timestamp = _make_error_func("timestamp", "Return POSIX timestamp as float.") + timestamp = _make_error_func( + "timestamp", + """ + Return POSIX timestamp as float. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548') + >>> ts.timestamp() + 1584199972.192548 + """ + ) # GH9513 NaT methods (except to_datetime64) to raise, return np.nan, or # return NaT create functions that raise, for binding to NaTType @@ -516,6 +589,29 @@ class NaTType(_NaT): ------ TypeError If Timestamp is tz-naive. + + Examples + -------- + Create a timestamp object with UTC timezone: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651', tz='UTC') + >>> ts + Timestamp('2020-03-14 15:32:52.192548651+0000', tz='UTC') + + Change to Tokyo timezone: + + >>> ts.tz_convert(tz='Asia/Tokyo') + Timestamp('2020-03-15 00:32:52.192548651+0900', tz='Asia/Tokyo') + + Can also use ``astimezone``: + + >>> ts.astimezone(tz='Asia/Tokyo') + Timestamp('2020-03-15 00:32:52.192548651+0900', tz='Asia/Tokyo') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.tz_convert(tz='Asia/Tokyo') + NaT """, ) fromordinal = _make_error_func( @@ -534,6 +630,11 @@ class NaTType(_NaT): Offset to apply to the Timestamp. tz : str, pytz.timezone, dateutil.tz.tzfile or None Time zone for the Timestamp. + + Examples + -------- + >>> pd.Timestamp.fromordinal(737425) + Timestamp('2020-01-01 00:00:00') """, ) @@ -544,6 +645,17 @@ class NaTType(_NaT): Convert a Timestamp object to a native Python datetime object. If warn=True, issue a warning if nanoseconds is nonzero. + + Examples + -------- + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548') + >>> ts.to_pydatetime() + datetime.datetime(2020, 3, 14, 15, 32, 52, 192548) + + Analogous for ``pd.NaT``: + + >>> pd.NaT.to_pydatetime() + NaT """, ) @@ -559,6 +671,16 @@ class NaTType(_NaT): ---------- tz : str or timezone object, default None Timezone to localize to. + + Examples + -------- + >>> pd.Timestamp.now() + Timestamp('2020-11-16 22:06:16.378782') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.now() + NaT """, ) today = _make_nat_func( @@ -574,6 +696,16 @@ class NaTType(_NaT): ---------- tz : str or timezone object, default None Timezone to localize to. + + Examples + -------- + >>> pd.Timestamp.today() + Timestamp('2020-11-16 22:37:39.969883') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.today() + NaT """, ) round = _make_nat_func( @@ -617,6 +749,41 @@ timedelta}, default 'raise' Raises ------ ValueError if the freq cannot be converted + + Examples + -------- + Create a timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + + A timestamp can be rounded using multiple frequency units: + + >>> ts.round(freq='H') # hour + Timestamp('2020-03-14 16:00:00') + + >>> ts.round(freq='T') # minute + Timestamp('2020-03-14 15:33:00') + + >>> ts.round(freq='S') # seconds + Timestamp('2020-03-14 15:32:52') + + >>> ts.round(freq='L') # milliseconds + Timestamp('2020-03-14 15:32:52.193000') + + ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): + + >>> ts.round(freq='5T') + Timestamp('2020-03-14 15:35:00') + + or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): + + >>> ts.round(freq='1H30T') + Timestamp('2020-03-14 15:00:00') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.round() + NaT """, ) floor = _make_nat_func( @@ -656,6 +823,41 @@ timedelta}, default 'raise' Raises ------ ValueError if the freq cannot be converted. + + Examples + -------- + Create a timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + + A timestamp can be floored using multiple frequency units: + + >>> ts.floor(freq='H') # hour + Timestamp('2020-03-14 15:00:00') + + >>> ts.floor(freq='T') # minute + Timestamp('2020-03-14 15:32:00') + + >>> ts.floor(freq='S') # seconds + Timestamp('2020-03-14 15:32:52') + + >>> ts.floor(freq='N') # nanoseconds + Timestamp('2020-03-14 15:32:52.192548651') + + ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): + + >>> ts.floor(freq='5T') + Timestamp('2020-03-14 15:30:00') + + or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): + + >>> ts.floor(freq='1H30T') + Timestamp('2020-03-14 15:00:00') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.floor() + NaT """, ) ceil = _make_nat_func( @@ -695,6 +897,41 @@ timedelta}, default 'raise' Raises ------ ValueError if the freq cannot be converted. + + Examples + -------- + Create a timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + + A timestamp can be ceiled using multiple frequency units: + + >>> ts.ceil(freq='H') # hour + Timestamp('2020-03-14 16:00:00') + + >>> ts.ceil(freq='T') # minute + Timestamp('2020-03-14 15:33:00') + + >>> ts.ceil(freq='S') # seconds + Timestamp('2020-03-14 15:32:53') + + >>> ts.ceil(freq='U') # microseconds + Timestamp('2020-03-14 15:32:52.192549') + + ``freq`` can also be a multiple of a single unit, like '5T' (i.e. 5 minutes): + + >>> ts.ceil(freq='5T') + Timestamp('2020-03-14 15:35:00') + + or a combination of multiple units, like '1H30T' (i.e. 1 hour and 30 minutes): + + >>> ts.ceil(freq='1H30T') + Timestamp('2020-03-14 16:30:00') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.ceil() + NaT """, ) @@ -717,6 +954,29 @@ timedelta}, default 'raise' ------ TypeError If Timestamp is tz-naive. + + Examples + -------- + Create a timestamp object with UTC timezone: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651', tz='UTC') + >>> ts + Timestamp('2020-03-14 15:32:52.192548651+0000', tz='UTC') + + Change to Tokyo timezone: + + >>> ts.tz_convert(tz='Asia/Tokyo') + Timestamp('2020-03-15 00:32:52.192548651+0900', tz='Asia/Tokyo') + + Can also use ``astimezone``: + + >>> ts.astimezone(tz='Asia/Tokyo') + Timestamp('2020-03-15 00:32:52.192548651+0900', tz='Asia/Tokyo') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.tz_convert(tz='Asia/Tokyo') + NaT """, ) tz_localize = _make_nat_func( @@ -772,6 +1032,24 @@ default 'raise' ------ TypeError If the Timestamp is tz-aware and tz is not None. + + Examples + -------- + Create a naive timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') + >>> ts + Timestamp('2020-03-14 15:32:52.192548651') + + Add 'Europe/Stockholm' as timezone: + + >>> ts.tz_localize(tz='Europe/Stockholm') + Timestamp('2020-03-14 15:32:52.192548651+0100', tz='Europe/Stockholm' + + Analogous for ``pd.NaT``: + + >>> pd.NaT.tz_localize() + NaT """, ) replace = _make_nat_func( @@ -795,6 +1073,30 @@ default 'raise' Returns ------- Timestamp with fields replaced + + Examples + -------- + Create a timestamp object: + + >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651', tz='UTC') + >>> ts + Timestamp('2020-03-14 15:32:52.192548651+0000', tz='UTC') + + Replace year and the hour: + + >>> ts.replace(year=1999, hour=10) + Timestamp('1999-03-14 10:32:52.192548651+0000', tz='UTC') + + Replace timezone (not a conversion): + + >>> import pytz + >>> ts.replace(tzinfo=pytz.timezone('US/Pacific')) + Timestamp('2020-03-14 15:32:52.192548651-0700', tz='US/Pacific') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.replace(tzinfo=pytz.timezone('US/Pacific')) + NaT """, ) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 80c37a1f20b38..758973332299d 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -580,6 +580,11 @@ cdef class _Timestamp(ABCTimestamp): >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') >>> ts.day_name() 'Saturday' + + Analogous for ``pd.NaT``: + + >>> pd.NaT.day_name() + nan """ return self._get_date_name_field("day_name", locale) @@ -601,6 +606,11 @@ cdef class _Timestamp(ABCTimestamp): >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') >>> ts.month_name() 'March' + + Analogous for ``pd.NaT``: + + >>> pd.NaT.month_name() + nan """ return self._get_date_name_field("month_name", locale) @@ -839,6 +849,11 @@ cdef class _Timestamp(ABCTimestamp): >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548') >>> ts.to_pydatetime() datetime.datetime(2020, 3, 14, 15, 32, 52, 192548) + + Analogous for ``pd.NaT``: + + >>> pd.NaT.to_pydatetime() + NaT """ if self.nanosecond != 0 and warn: warnings.warn("Discarding nonzero nanoseconds in conversion", @@ -877,6 +892,11 @@ cdef class _Timestamp(ABCTimestamp): >>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') >>> ts.to_numpy() numpy.datetime64('2020-03-14T15:32:52.192548651') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.to_numpy() + numpy.datetime64('NaT') """ return self.to_datetime64() @@ -1033,6 +1053,11 @@ class Timestamp(_Timestamp): -------- >>> pd.Timestamp.now() Timestamp('2020-11-16 22:06:16.378782') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.now() + NaT """ if isinstance(tz, str): tz = maybe_get_tz(tz) @@ -1056,6 +1081,11 @@ class Timestamp(_Timestamp): -------- >>> pd.Timestamp.today() Timestamp('2020-11-16 22:37:39.969883') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.today() + NaT """ return cls.now(tz) @@ -1391,6 +1421,11 @@ timedelta}, default 'raise' >>> ts.round(freq='1H30T') Timestamp('2020-03-14 15:00:00') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.round() + NaT """ return self._round( freq, RoundTo.NEAREST_HALF_EVEN, ambiguous, nonexistent @@ -1462,6 +1497,11 @@ timedelta}, default 'raise' >>> ts.floor(freq='1H30T') Timestamp('2020-03-14 15:00:00') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.floor() + NaT """ return self._round(freq, RoundTo.MINUS_INFTY, ambiguous, nonexistent) @@ -1531,6 +1571,11 @@ timedelta}, default 'raise' >>> ts.ceil(freq='1H30T') Timestamp('2020-03-14 16:30:00') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.ceil() + NaT """ return self._round(freq, RoundTo.PLUS_INFTY, ambiguous, nonexistent) @@ -1626,7 +1671,12 @@ default 'raise' Add 'Europe/Stockholm' as timezone: >>> ts.tz_localize(tz='Europe/Stockholm') - Timestamp('2020-03-14 15:32:52.192548651+0100', tz='Europe/Stockholm') + Timestamp('2020-03-14 15:32:52.192548651+0100', tz='Europe/Stockholm' + + Analogous for ``pd.NaT``: + + >>> pd.NaT.tz_localize() + NaT """ if ambiguous == 'infer': raise ValueError('Cannot infer offset with only one time.') @@ -1688,6 +1738,16 @@ default 'raise' >>> ts.tz_convert(tz='Asia/Tokyo') Timestamp('2020-03-15 00:32:52.192548651+0900', tz='Asia/Tokyo') + + Can also use ``astimezone``: + + >>> ts.astimezone(tz='Asia/Tokyo') + Timestamp('2020-03-15 00:32:52.192548651+0900', tz='Asia/Tokyo') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.tz_convert(tz='Asia/Tokyo') + NaT """ if self.tzinfo is None: # tz naive, use tz_localize @@ -1751,6 +1811,11 @@ default 'raise' >>> import pytz >>> ts.replace(tzinfo=pytz.timezone('US/Pacific')) Timestamp('2020-03-14 15:32:52.192548651-0700', tz='US/Pacific') + + Analogous for ``pd.NaT``: + + >>> pd.NaT.replace(tzinfo=pytz.timezone('US/Pacific')) + NaT """ cdef: From 83edb1c82ad87281d5107f3aaec02452bf668fca Mon Sep 17 00:00:00 2001 From: lucasrodes Date: Fri, 27 Nov 2020 10:35:05 +0000 Subject: [PATCH 18/20] trigger GitHub actions From c622449e2d5e5934be64508b2a11348f4303e73a Mon Sep 17 00:00:00 2001 From: lucas rg Date: Sun, 21 Mar 2021 10:58:58 +0000 Subject: [PATCH 19/20] added missing ( in docstring examples --- pandas/_libs/tslibs/nattype.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx index 0231e856a83b5..8069d8b5cacc8 100644 --- a/pandas/_libs/tslibs/nattype.pyx +++ b/pandas/_libs/tslibs/nattype.pyx @@ -1059,7 +1059,7 @@ default 'raise' Add 'Europe/Stockholm' as timezone: >>> ts.tz_localize(tz='Europe/Stockholm') - Timestamp('2020-03-14 15:32:52.192548651+0100', tz='Europe/Stockholm' + Timestamp('2020-03-14 15:32:52.192548651+0100', tz='Europe/Stockholm') Analogous for ``pd.NaT``: From bcbf195899dae6beb6e9bd099afa6cb83f440403 Mon Sep 17 00:00:00 2001 From: lucas rg Date: Sun, 21 Mar 2021 11:04:24 +0000 Subject: [PATCH 20/20] added missing ( in docstring example --- pandas/_libs/tslibs/timestamps.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index f587818755267..a4f764878d19e 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -1622,7 +1622,7 @@ default 'raise' Add 'Europe/Stockholm' as timezone: >>> ts.tz_localize(tz='Europe/Stockholm') - Timestamp('2020-03-14 15:32:52.192548651+0100', tz='Europe/Stockholm' + Timestamp('2020-03-14 15:32:52.192548651+0100', tz='Europe/Stockholm') Analogous for ``pd.NaT``: