Skip to content

Commit a2bbdb5

Browse files
ShaharNavehtopper-123
authored andcommitted
STY: Underscores for long numbers (#30397)
1 parent 835f207 commit a2bbdb5

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed

pandas/_libs/tslibs/timestamps.pyx

+18-7
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

+28-28
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
@@ -807,8 +807,8 @@ def test_basics_nanos(self):
807807
[
808808
[946688461000000000, {}],
809809
[946688461000000000 / 1000, dict(unit="us")],
810-
[946688461000000000 / 1000000, dict(unit="ms")],
811-
[946688461000000000 / 1000000000, dict(unit="s")],
810+
[946688461000000000 / 1_000_000, dict(unit="ms")],
811+
[946688461000000000 / 1_000_000_000, dict(unit="s")],
812812
[10957, dict(unit="D", h=0)],
813813
[
814814
(946688461000000000 + 500000) / 1000000000,
@@ -852,24 +852,24 @@ def test_roundtrip(self):
852852
base = Timestamp("20140101 00:00:00")
853853

854854
result = Timestamp(base.value + Timedelta("5ms").value)
855-
assert result == Timestamp(str(base) + ".005000")
855+
assert result == Timestamp(f"{base}.005000")
856856
assert result.microsecond == 5000
857857

858858
result = Timestamp(base.value + Timedelta("5us").value)
859-
assert result == Timestamp(str(base) + ".000005")
859+
assert result == Timestamp(f"{base}.000005")
860860
assert result.microsecond == 5
861861

862862
result = Timestamp(base.value + Timedelta("5ns").value)
863-
assert result == Timestamp(str(base) + ".000000005")
863+
assert result == Timestamp(f"{base}.000000005")
864864
assert result.nanosecond == 5
865865
assert result.microsecond == 0
866866

867867
result = Timestamp(base.value + Timedelta("6ms 5us").value)
868-
assert result == Timestamp(str(base) + ".006005")
868+
assert result == Timestamp(f"{base}.006005")
869869
assert result.microsecond == 5 + 6 * 1000
870870

871871
result = Timestamp(base.value + Timedelta("200ms 5us").value)
872-
assert result == Timestamp(str(base) + ".200005")
872+
assert result == Timestamp(f"{base}.200005")
873873
assert result.microsecond == 5 + 200 * 1000
874874

875875
def test_hash_equivalent(self):
@@ -890,20 +890,20 @@ def test_nanosecond_string_parsing(self):
890890
ts = Timestamp("2013-05-01 07:15:45.123456789")
891891
# GH 7878
892892
expected_repr = "2013-05-01 07:15:45.123456789"
893-
expected_value = 1367392545123456789
893+
expected_value = 1_367_392_545_123_456_789
894894
assert ts.value == expected_value
895895
assert expected_repr in repr(ts)
896896

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

901901
ts = Timestamp("2013-05-01 07:15:45.123456789", tz="UTC")
902902
assert ts.value == expected_value
903903
assert expected_repr in repr(ts)
904904

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

909909
# GH 10041
@@ -913,7 +913,7 @@ def test_nanosecond_string_parsing(self):
913913

914914
def test_nanosecond_timestamp(self):
915915
# GH 7610
916-
expected = 1293840000000000005
916+
expected = 1_293_840_000_000_000_005
917917
t = Timestamp("2011-01-01") + offsets.Nano(5)
918918
assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000005')"
919919
assert t.value == expected
@@ -929,7 +929,7 @@ def test_nanosecond_timestamp(self):
929929
assert t.value == expected
930930
assert t.nanosecond == 5
931931

932-
expected = 1293840000000000010
932+
expected = 1_293_840_000_000_000_010
933933
t = t + offsets.Nano(5)
934934
assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000010')"
935935
assert t.value == expected
@@ -949,23 +949,23 @@ def test_nanosecond_timestamp(self):
949949
class TestTimestampToJulianDate:
950950
def test_compare_1700(self):
951951
r = Timestamp("1700-06-23").to_julian_date()
952-
assert r == 2342145.5
952+
assert r == 2_342_145.5
953953

954954
def test_compare_2000(self):
955955
r = Timestamp("2000-04-12").to_julian_date()
956-
assert r == 2451646.5
956+
assert r == 2_451_646.5
957957

958958
def test_compare_2100(self):
959959
r = Timestamp("2100-08-12").to_julian_date()
960-
assert r == 2488292.5
960+
assert r == 2_488_292.5
961961

962962
def test_compare_hour01(self):
963963
r = Timestamp("2000-08-12T01:00:00").to_julian_date()
964-
assert r == 2451768.5416666666666666
964+
assert r == 2_451_768.5416666666666666
965965

966966
def test_compare_hour13(self):
967967
r = Timestamp("2000-08-12T13:00:00").to_julian_date()
968-
assert r == 2451769.0416666666666666
968+
assert r == 2_451_769.0416666666666666
969969

970970

971971
class TestTimestampConversion:

0 commit comments

Comments
 (0)