Skip to content

Commit 01fc0b5

Browse files
author
MomIsBestFriend
committed
STY: Underscores for long numbers
1 parent 477b2d5 commit 01fc0b5

File tree

2 files changed

+70
-47
lines changed

2 files changed

+70
-47
lines changed

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,22 @@ class Timestamp(_Timestamp):
336336
"""
337337
return cls(datetime.combine(date, time))
338338

339-
def __new__(cls, object ts_input=_no_input,
340-
object freq=None, tz=None, unit=None,
341-
year=None, month=None, day=None,
342-
hour=None, minute=None, second=None, microsecond=None,
343-
nanosecond=None, tzinfo=None):
339+
def __new__(
340+
cls,
341+
object ts_input=_no_input,
342+
object freq=None,
343+
tz=None,
344+
unit=None,
345+
year=None,
346+
month=None,
347+
day=None,
348+
hour=None,
349+
minute=None,
350+
second=None,
351+
microsecond=None,
352+
nanosecond=None,
353+
tzinfo=None
354+
):
344355
# The parameter list folds together legacy parameter names (the first
345356
# four) and positional and keyword parameter names from pydatetime.
346357
#
@@ -401,8 +412,8 @@ class Timestamp(_Timestamp):
401412
freq = None
402413

403414
if getattr(ts_input, 'tzinfo', None) is not None and tz is not None:
404-
raise ValueError("Cannot pass a datetime or Timestamp with tzinfo with the"
405-
" tz parameter. Use tz_convert instead.")
415+
raise ValueError("Cannot pass a datetime or Timestamp with tzinfo with "
416+
"the tz parameter. Use tz_convert instead.")
406417

407418
ts = convert_to_tsobject(ts_input, tz, unit, 0, 0, nanosecond or 0)
408419

pandas/tests/scalar/timestamp/test_timestamp.py

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,17 @@ class TestTimestampConstructors:
201201
def test_constructor(self):
202202
base_str = "2014-07-01 09:00"
203203
base_dt = datetime(2014, 7, 1, 9)
204-
base_expected = 1404205200000000000
204+
base_expected = 1_404_205_200_000_000_000
205205

206206
# confirm base representation is correct
207-
assert calendar.timegm(base_dt.timetuple()) * 1000000000 == base_expected
207+
assert calendar.timegm(base_dt.timetuple()) * 1_000_000_000 == base_expected
208208

209209
tests = [
210210
(base_str, base_dt, base_expected),
211211
(
212212
"2014-07-01 10:00",
213213
datetime(2014, 7, 1, 10),
214-
base_expected + 3600 * 1000000000,
214+
base_expected + 3600 * 1_000_000_000,
215215
),
216216
(
217217
"2014-07-01 09:00:00.000008000",
@@ -250,7 +250,7 @@ def test_constructor(self):
250250
# with timezone
251251
for tz, offset in timezones:
252252
for result in [Timestamp(date_str, tz=tz), Timestamp(date, tz=tz)]:
253-
expected_tz = expected - offset * 3600 * 1000000000
253+
expected_tz = expected - offset * 3600 * 1_000_000_000
254254
assert result.value == expected_tz
255255
assert conversion.pydt_to_i8(result) == expected_tz
256256

@@ -264,22 +264,22 @@ def test_constructor(self):
264264
result = Timestamp(result).tz_convert("UTC")
265265
else:
266266
result = Timestamp(result, tz="UTC")
267-
expected_utc = expected - offset * 3600 * 1000000000
267+
expected_utc = expected - offset * 3600 * 1_000_000_000
268268
assert result.value == expected_utc
269269
assert conversion.pydt_to_i8(result) == expected_utc
270270

271271
def test_constructor_with_stringoffset(self):
272272
# GH 7833
273273
base_str = "2014-07-01 11:00:00+02:00"
274274
base_dt = datetime(2014, 7, 1, 9)
275-
base_expected = 1404205200000000000
275+
base_expected = 1_404_205_200_000_000_000
276276

277277
# confirm base representation is correct
278-
assert calendar.timegm(base_dt.timetuple()) * 1000000000 == base_expected
278+
assert calendar.timegm(base_dt.timetuple()) * 1_000_000_000 == base_expected
279279

280280
tests = [
281281
(base_str, base_expected),
282-
("2014-07-01 12:00:00+02:00", base_expected + 3600 * 1000000000),
282+
("2014-07-01 12:00:00+02:00", base_expected + 3600 * 1_000_000_000),
283283
("2014-07-01 11:00:00.000008000+02:00", base_expected + 8000),
284284
("2014-07-01 11:00:00.000000005+02:00", base_expected + 5),
285285
]
@@ -725,7 +725,7 @@ def test_utc_z_designator(self):
725725
assert get_timezone(Timestamp("2014-11-02 01:00Z").tzinfo) is utc
726726

727727
def test_asm8(self):
728-
np.random.seed(7960929)
728+
np.random.seed(7_960_929)
729729
ns = [Timestamp.min.value, Timestamp.max.value, 1000]
730730

731731
for n in ns:
@@ -786,15 +786,15 @@ def compare(x, y):
786786
)
787787

788788
def test_basics_nanos(self):
789-
val = np.int64(946684800000000000).view("M8[ns]")
789+
val = np.int64(946_684_800_000_000_000).view("M8[ns]")
790790
stamp = Timestamp(val.view("i8") + 500)
791791
assert stamp.year == 2000
792792
assert stamp.month == 1
793793
assert stamp.microsecond == 0
794794
assert stamp.nanosecond == 500
795795

796796
# GH 14415
797-
val = np.iinfo(np.int64).min + 80000000000000
797+
val = np.iinfo(np.int64).min + 80_000_000_000_000
798798
stamp = Timestamp(val)
799799
assert stamp.year == 1677
800800
assert stamp.month == 9
@@ -805,24 +805,36 @@ def test_basics_nanos(self):
805805
@pytest.mark.parametrize(
806806
"value, check_kwargs",
807807
[
808-
[946688461000000000, {}],
809-
[946688461000000000 / 1000, dict(unit="us")],
810-
[946688461000000000 / 1000000, dict(unit="ms")],
811-
[946688461000000000 / 1000000000, dict(unit="s")],
808+
[946_688_461_000_000_000, {}],
809+
[946_688_461_000_000_000 / 1000, dict(unit="us")],
810+
[946_688_461_000_000_000 / 1_000_000, dict(unit="ms")],
811+
[946_688_461_000_000_000 / 1_000_000_000, dict(unit="s")],
812812
[10957, dict(unit="D", h=0)],
813813
[
814-
(946688461000000000 + 500000) / 1000000000,
814+
(946_688_461_000_000_000 + 500000) / 1_000_000_000,
815815
dict(unit="s", us=499, ns=964),
816816
],
817-
[(946688461000000000 + 500000000) / 1000000000, dict(unit="s", us=500000)],
818-
[(946688461000000000 + 500000) / 1000000, dict(unit="ms", us=500)],
819-
[(946688461000000000 + 500000) / 1000, dict(unit="us", us=500)],
820-
[(946688461000000000 + 500000000) / 1000000, dict(unit="ms", us=500000)],
821-
[946688461000000000 / 1000.0 + 5, dict(unit="us", us=5)],
822-
[946688461000000000 / 1000.0 + 5000, dict(unit="us", us=5000)],
823-
[946688461000000000 / 1000000.0 + 0.5, dict(unit="ms", us=500)],
824-
[946688461000000000 / 1000000.0 + 0.005, dict(unit="ms", us=5, ns=5)],
825-
[946688461000000000 / 1000000000.0 + 0.5, dict(unit="s", us=500000)],
817+
[
818+
(946_688_461_000_000_000 + 500_000_000) / 1_000_000_000,
819+
dict(unit="s", us=500000),
820+
],
821+
[(946_688_461_000_000_000 + 500000) / 1_000_000, dict(unit="ms", us=500)],
822+
[(946_688_461_000_000_000 + 500000) / 1000, dict(unit="us", us=500)],
823+
[
824+
(946_688_461_000_000_000 + 500_000_000) / 1_000_000,
825+
dict(unit="ms", us=500000),
826+
],
827+
[946_688_461_000_000_000 / 1000.0 + 5, dict(unit="us", us=5)],
828+
[946_688_461_000_000_000 / 1000.0 + 5000, dict(unit="us", us=5000)],
829+
[946_688_461_000_000_000 / 1_000_000.0 + 0.5, dict(unit="ms", us=500)],
830+
[
831+
946_688_461_000_000_000 / 1_000_000.0 + 0.005,
832+
dict(unit="ms", us=5, ns=5),
833+
],
834+
[
835+
946_688_461_000_000_000 / 1_000_000_000.0 + 0.5,
836+
dict(unit="s", us=500000),
837+
],
826838
[10957 + 0.5, dict(unit="D", h=12)],
827839
],
828840
)
@@ -852,24 +864,24 @@ def test_roundtrip(self):
852864
base = Timestamp("20140101 00:00:00")
853865

854866
result = Timestamp(base.value + Timedelta("5ms").value)
855-
assert result == Timestamp(str(base) + ".005000")
867+
assert result == Timestamp(f"{base}.005000")
856868
assert result.microsecond == 5000
857869

858870
result = Timestamp(base.value + Timedelta("5us").value)
859-
assert result == Timestamp(str(base) + ".000005")
871+
assert result == Timestamp(f"{base}.000005")
860872
assert result.microsecond == 5
861873

862874
result = Timestamp(base.value + Timedelta("5ns").value)
863-
assert result == Timestamp(str(base) + ".000000005")
875+
assert result == Timestamp(f"{base}.000000005")
864876
assert result.nanosecond == 5
865877
assert result.microsecond == 0
866878

867879
result = Timestamp(base.value + Timedelta("6ms 5us").value)
868-
assert result == Timestamp(str(base) + ".006005")
880+
assert result == Timestamp(f"{base}.006005")
869881
assert result.microsecond == 5 + 6 * 1000
870882

871883
result = Timestamp(base.value + Timedelta("200ms 5us").value)
872-
assert result == Timestamp(str(base) + ".200005")
884+
assert result == Timestamp(f"{base}.200005")
873885
assert result.microsecond == 5 + 200 * 1000
874886

875887
def test_hash_equivalent(self):
@@ -890,20 +902,20 @@ def test_nanosecond_string_parsing(self):
890902
ts = Timestamp("2013-05-01 07:15:45.123456789")
891903
# GH 7878
892904
expected_repr = "2013-05-01 07:15:45.123456789"
893-
expected_value = 1367392545123456789
905+
expected_value = 1_367_392_545_123_456_789
894906
assert ts.value == expected_value
895907
assert expected_repr in repr(ts)
896908

897909
ts = Timestamp("2013-05-01 07:15:45.123456789+09:00", tz="Asia/Tokyo")
898-
assert ts.value == expected_value - 9 * 3600 * 1000000000
910+
assert ts.value == expected_value - 9 * 3600 * 1_000_000_000
899911
assert expected_repr in repr(ts)
900912

901913
ts = Timestamp("2013-05-01 07:15:45.123456789", tz="UTC")
902914
assert ts.value == expected_value
903915
assert expected_repr in repr(ts)
904916

905917
ts = Timestamp("2013-05-01 07:15:45.123456789", tz="US/Eastern")
906-
assert ts.value == expected_value + 4 * 3600 * 1000000000
918+
assert ts.value == expected_value + 4 * 3600 * 1_000_000_000
907919
assert expected_repr in repr(ts)
908920

909921
# GH 10041
@@ -913,7 +925,7 @@ def test_nanosecond_string_parsing(self):
913925

914926
def test_nanosecond_timestamp(self):
915927
# GH 7610
916-
expected = 1293840000000000005
928+
expected = 1_293_840_000_000_000_005
917929
t = Timestamp("2011-01-01") + offsets.Nano(5)
918930
assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000005')"
919931
assert t.value == expected
@@ -929,7 +941,7 @@ def test_nanosecond_timestamp(self):
929941
assert t.value == expected
930942
assert t.nanosecond == 5
931943

932-
expected = 1293840000000000010
944+
expected = 1_293_840_000_000_000_010
933945
t = t + offsets.Nano(5)
934946
assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000010')"
935947
assert t.value == expected
@@ -949,23 +961,23 @@ def test_nanosecond_timestamp(self):
949961
class TestTimestampToJulianDate:
950962
def test_compare_1700(self):
951963
r = Timestamp("1700-06-23").to_julian_date()
952-
assert r == 2342145.5
964+
assert r == 2_342_145.5
953965

954966
def test_compare_2000(self):
955967
r = Timestamp("2000-04-12").to_julian_date()
956-
assert r == 2451646.5
968+
assert r == 2_451_646.5
957969

958970
def test_compare_2100(self):
959971
r = Timestamp("2100-08-12").to_julian_date()
960-
assert r == 2488292.5
972+
assert r == 2_488_292.5
961973

962974
def test_compare_hour01(self):
963975
r = Timestamp("2000-08-12T01:00:00").to_julian_date()
964-
assert r == 2451768.5416666666666666
976+
assert r == 2_451_768.5416666666666666
965977

966978
def test_compare_hour13(self):
967979
r = Timestamp("2000-08-12T13:00:00").to_julian_date()
968-
assert r == 2451769.0416666666666666
980+
assert r == 2_451_769.0416666666666666
969981

970982

971983
class TestTimestampConversion:

0 commit comments

Comments
 (0)