Skip to content

parameterize tests in scalar/timedelta #20428

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 5 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 14 additions & 20 deletions pandas/tests/scalar/timedelta/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,28 +195,22 @@ def test_iso_constructor_raises(fmt):
Timedelta(fmt)


def test_td_constructor_on_nanoseconds():
@pytest.mark.parametrize('constructed_td, conversion',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just stylistically if you put the left bracket to start the list of parameters on the first line it could make this more readable (reduces the indentation required for every subsequent line)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very good point. thanks!

[(Timedelta(nanoseconds=100), '100ns'),
(Timedelta(days=1, hours=1, minutes=1, weeks=1,
seconds=1, milliseconds=1, microseconds=1,
nanoseconds=1), 694861001001001),
(Timedelta(microseconds=1) +
Timedelta(nanoseconds=1), '1us1ns'),
(Timedelta(microseconds=1) -
Timedelta(nanoseconds=1), '999ns'),
(Timedelta(microseconds=1) +
5 * Timedelta(nanoseconds=-2), '990ns')])
def test_td_constructor_on_nanoseconds(constructed_td, conversion):
# GH#9273
result = Timedelta(nanoseconds=100)
expected = Timedelta('100ns')
assert result == expected

result = Timedelta(days=1, hours=1, minutes=1, weeks=1, seconds=1,
milliseconds=1, microseconds=1, nanoseconds=1)
expected = Timedelta(694861001001001)
assert result == expected

result = Timedelta(microseconds=1) + Timedelta(nanoseconds=1)
expected = Timedelta('1us1ns')
assert result == expected

result = Timedelta(microseconds=1) - Timedelta(nanoseconds=1)
expected = Timedelta('999ns')
assert result == expected
assert constructed_td == Timedelta(conversion)

result = Timedelta(microseconds=1) + 5 * Timedelta(nanoseconds=-2)
expected = Timedelta('990ns')
assert result == expected

def test_td_constructor_value_error():
with pytest.raises(TypeError):
Timedelta(nanoseconds='abc')
79 changes: 35 additions & 44 deletions pandas/tests/scalar/timedelta/test_formats.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,39 @@
# -*- coding: utf-8 -*-
from pandas import Timedelta


def test_repr():
assert (repr(Timedelta(10, unit='d')) ==
"Timedelta('10 days 00:00:00')")
assert (repr(Timedelta(10, unit='s')) ==
"Timedelta('0 days 00:00:10')")
assert (repr(Timedelta(10, unit='ms')) ==
"Timedelta('0 days 00:00:00.010000')")
assert (repr(Timedelta(-10, unit='ms')) ==
"Timedelta('-1 days +23:59:59.990000')")

import pytest

def test_isoformat():
td = Timedelta(days=6, minutes=50, seconds=3,
milliseconds=10, microseconds=10, nanoseconds=12)
expected = 'P6DT0H50M3.010010012S'
result = td.isoformat()
assert result == expected

td = Timedelta(days=4, hours=12, minutes=30, seconds=5)
result = td.isoformat()
expected = 'P4DT12H30M5S'
assert result == expected

td = Timedelta(nanoseconds=123)
result = td.isoformat()
expected = 'P0DT0H0M0.000000123S'
assert result == expected

# trim nano
td = Timedelta(microseconds=10)
result = td.isoformat()
expected = 'P0DT0H0M0.00001S'
assert result == expected
from pandas import Timedelta

# trim micro
td = Timedelta(milliseconds=1)
result = td.isoformat()
expected = 'P0DT0H0M0.001S'
assert result == expected

# don't strip every 0
result = Timedelta(minutes=1).isoformat()
expected = 'P0DT0H1M0S'
assert result == expected
@pytest.mark.parametrize('td, expected_repr',
[(Timedelta(10, unit='d'),
"Timedelta('10 days 00:00:00')"),
(Timedelta(10, unit='s'),
"Timedelta('0 days 00:00:10')"),
(Timedelta(10, unit='ms'),
"Timedelta('0 days 00:00:00.010000')"),
(Timedelta(-10, unit='ms'),
"Timedelta('-1 days +23:59:59.990000')")])
def test_repr(td, expected_repr):
assert repr(td) == expected_repr


@pytest.mark.parametrize('td, expected_iso',
[(Timedelta(days=6, minutes=50, seconds=3,
milliseconds=10, microseconds=10,
nanoseconds=12),
'P6DT0H50M3.010010012S'),
(Timedelta(days=4, hours=12, minutes=30, seconds=5),
'P4DT12H30M5S'),
(Timedelta(nanoseconds=123),
'P0DT0H0M0.000000123S'),
# trim nano
(Timedelta(microseconds=10),
'P0DT0H0M0.00001S'),
# trim micro
(Timedelta(milliseconds=1),
'P0DT0H0M0.001S'),
# don't strip every 0
(Timedelta(minutes=1),
'P0DT0H1M0S')])
def test_isoformat(td, expected_iso):
assert td.isoformat() == expected_iso