Skip to content

STY: Underscores for long numbers #30397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down Expand Up @@ -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)

Expand Down
56 changes: 28 additions & 28 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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

Expand All @@ -264,22 +264,22 @@ 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

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),
]
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -786,15 +786,15 @@ 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
assert stamp.microsecond == 0
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
Expand All @@ -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,
Expand Down Expand Up @@ -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):
Expand All @@ -890,20 +890,20 @@ 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")
assert ts.value == expected_value
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down