From 0d7540ba405e57260f571f035a8fd22512872c49 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 22 Dec 2019 14:18:29 +0200 Subject: [PATCH] STY: Underscores for long numbers --- pandas/_libs/tslibs/timestamps.pyx | 25 ++++++--- .../tests/scalar/timestamp/test_timestamp.py | 56 +++++++++---------- 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index a44f374264f09..86a9d053730b8 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -336,11 +336,22 @@ class Timestamp(_Timestamp): """ return cls(datetime.combine(date, time)) - def __new__(cls, object ts_input=_no_input, - object freq=None, tz=None, unit=None, - year=None, month=None, day=None, - hour=None, minute=None, second=None, microsecond=None, - nanosecond=None, tzinfo=None): + def __new__( + cls, + object ts_input=_no_input, + object freq=None, + tz=None, + unit=None, + year=None, + month=None, + day=None, + hour=None, + minute=None, + second=None, + microsecond=None, + nanosecond=None, + tzinfo=None + ): # The parameter list folds together legacy parameter names (the first # four) and positional and keyword parameter names from pydatetime. # @@ -401,8 +412,8 @@ class Timestamp(_Timestamp): freq = None if getattr(ts_input, 'tzinfo', None) is not None and tz is not None: - raise ValueError("Cannot pass a datetime or Timestamp with tzinfo with the" - " tz parameter. Use tz_convert instead.") + raise ValueError("Cannot pass a datetime or Timestamp with tzinfo with " + "the tz parameter. Use tz_convert instead.") ts = convert_to_tsobject(ts_input, tz, unit, 0, 0, nanosecond or 0) diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index 512a83ed304d1..25609cb852ed4 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -201,17 +201,17 @@ class TestTimestampConstructors: def test_constructor(self): base_str = "2014-07-01 09:00" base_dt = datetime(2014, 7, 1, 9) - base_expected = 1404205200000000000 + base_expected = 1_404_205_200_000_000_000 # confirm base representation is correct - assert calendar.timegm(base_dt.timetuple()) * 1000000000 == base_expected + assert calendar.timegm(base_dt.timetuple()) * 1_000_000_000 == base_expected tests = [ (base_str, base_dt, base_expected), ( "2014-07-01 10:00", datetime(2014, 7, 1, 10), - base_expected + 3600 * 1000000000, + base_expected + 3600 * 1_000_000_000, ), ( "2014-07-01 09:00:00.000008000", @@ -250,7 +250,7 @@ def test_constructor(self): # with timezone for tz, offset in timezones: for result in [Timestamp(date_str, tz=tz), Timestamp(date, tz=tz)]: - expected_tz = expected - offset * 3600 * 1000000000 + expected_tz = expected - offset * 3600 * 1_000_000_000 assert result.value == expected_tz assert conversion.pydt_to_i8(result) == expected_tz @@ -264,7 +264,7 @@ def test_constructor(self): result = Timestamp(result).tz_convert("UTC") else: result = Timestamp(result, tz="UTC") - expected_utc = expected - offset * 3600 * 1000000000 + expected_utc = expected - offset * 3600 * 1_000_000_000 assert result.value == expected_utc assert conversion.pydt_to_i8(result) == expected_utc @@ -272,14 +272,14 @@ def test_constructor_with_stringoffset(self): # GH 7833 base_str = "2014-07-01 11:00:00+02:00" base_dt = datetime(2014, 7, 1, 9) - base_expected = 1404205200000000000 + base_expected = 1_404_205_200_000_000_000 # confirm base representation is correct - assert calendar.timegm(base_dt.timetuple()) * 1000000000 == base_expected + assert calendar.timegm(base_dt.timetuple()) * 1_000_000_000 == base_expected tests = [ (base_str, base_expected), - ("2014-07-01 12:00:00+02:00", base_expected + 3600 * 1000000000), + ("2014-07-01 12:00:00+02:00", base_expected + 3600 * 1_000_000_000), ("2014-07-01 11:00:00.000008000+02:00", base_expected + 8000), ("2014-07-01 11:00:00.000000005+02:00", base_expected + 5), ] @@ -725,7 +725,7 @@ def test_utc_z_designator(self): assert get_timezone(Timestamp("2014-11-02 01:00Z").tzinfo) is utc def test_asm8(self): - np.random.seed(7960929) + np.random.seed(7_960_929) ns = [Timestamp.min.value, Timestamp.max.value, 1000] for n in ns: @@ -786,7 +786,7 @@ def compare(x, y): ) def test_basics_nanos(self): - val = np.int64(946684800000000000).view("M8[ns]") + val = np.int64(946_684_800_000_000_000).view("M8[ns]") stamp = Timestamp(val.view("i8") + 500) assert stamp.year == 2000 assert stamp.month == 1 @@ -794,7 +794,7 @@ def test_basics_nanos(self): assert stamp.nanosecond == 500 # GH 14415 - val = np.iinfo(np.int64).min + 80000000000000 + val = np.iinfo(np.int64).min + 80_000_000_000_000 stamp = Timestamp(val) assert stamp.year == 1677 assert stamp.month == 9 @@ -807,8 +807,8 @@ def test_basics_nanos(self): [ [946688461000000000, {}], [946688461000000000 / 1000, dict(unit="us")], - [946688461000000000 / 1000000, dict(unit="ms")], - [946688461000000000 / 1000000000, dict(unit="s")], + [946688461000000000 / 1_000_000, dict(unit="ms")], + [946688461000000000 / 1_000_000_000, dict(unit="s")], [10957, dict(unit="D", h=0)], [ (946688461000000000 + 500000) / 1000000000, @@ -852,24 +852,24 @@ def test_roundtrip(self): base = Timestamp("20140101 00:00:00") result = Timestamp(base.value + Timedelta("5ms").value) - assert result == Timestamp(str(base) + ".005000") + assert result == Timestamp(f"{base}.005000") assert result.microsecond == 5000 result = Timestamp(base.value + Timedelta("5us").value) - assert result == Timestamp(str(base) + ".000005") + assert result == Timestamp(f"{base}.000005") assert result.microsecond == 5 result = Timestamp(base.value + Timedelta("5ns").value) - assert result == Timestamp(str(base) + ".000000005") + assert result == Timestamp(f"{base}.000000005") assert result.nanosecond == 5 assert result.microsecond == 0 result = Timestamp(base.value + Timedelta("6ms 5us").value) - assert result == Timestamp(str(base) + ".006005") + assert result == Timestamp(f"{base}.006005") assert result.microsecond == 5 + 6 * 1000 result = Timestamp(base.value + Timedelta("200ms 5us").value) - assert result == Timestamp(str(base) + ".200005") + assert result == Timestamp(f"{base}.200005") assert result.microsecond == 5 + 200 * 1000 def test_hash_equivalent(self): @@ -890,12 +890,12 @@ def test_nanosecond_string_parsing(self): ts = Timestamp("2013-05-01 07:15:45.123456789") # GH 7878 expected_repr = "2013-05-01 07:15:45.123456789" - expected_value = 1367392545123456789 + expected_value = 1_367_392_545_123_456_789 assert ts.value == expected_value assert expected_repr in repr(ts) ts = Timestamp("2013-05-01 07:15:45.123456789+09:00", tz="Asia/Tokyo") - assert ts.value == expected_value - 9 * 3600 * 1000000000 + assert ts.value == expected_value - 9 * 3600 * 1_000_000_000 assert expected_repr in repr(ts) ts = Timestamp("2013-05-01 07:15:45.123456789", tz="UTC") @@ -903,7 +903,7 @@ def test_nanosecond_string_parsing(self): assert expected_repr in repr(ts) ts = Timestamp("2013-05-01 07:15:45.123456789", tz="US/Eastern") - assert ts.value == expected_value + 4 * 3600 * 1000000000 + assert ts.value == expected_value + 4 * 3600 * 1_000_000_000 assert expected_repr in repr(ts) # GH 10041 @@ -913,7 +913,7 @@ def test_nanosecond_string_parsing(self): def test_nanosecond_timestamp(self): # GH 7610 - expected = 1293840000000000005 + expected = 1_293_840_000_000_000_005 t = Timestamp("2011-01-01") + offsets.Nano(5) assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000005')" assert t.value == expected @@ -929,7 +929,7 @@ def test_nanosecond_timestamp(self): assert t.value == expected assert t.nanosecond == 5 - expected = 1293840000000000010 + expected = 1_293_840_000_000_000_010 t = t + offsets.Nano(5) assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000010')" assert t.value == expected @@ -949,23 +949,23 @@ def test_nanosecond_timestamp(self): class TestTimestampToJulianDate: def test_compare_1700(self): r = Timestamp("1700-06-23").to_julian_date() - assert r == 2342145.5 + assert r == 2_342_145.5 def test_compare_2000(self): r = Timestamp("2000-04-12").to_julian_date() - assert r == 2451646.5 + assert r == 2_451_646.5 def test_compare_2100(self): r = Timestamp("2100-08-12").to_julian_date() - assert r == 2488292.5 + assert r == 2_488_292.5 def test_compare_hour01(self): r = Timestamp("2000-08-12T01:00:00").to_julian_date() - assert r == 2451768.5416666666666666 + assert r == 2_451_768.5416666666666666 def test_compare_hour13(self): r = Timestamp("2000-08-12T13:00:00").to_julian_date() - assert r == 2451769.0416666666666666 + assert r == 2_451_769.0416666666666666 class TestTimestampConversion: