Skip to content

Commit

Permalink
parameterize tests in scalar/timedelta (pandas-dev#20428)
Browse files Browse the repository at this point in the history
  • Loading branch information
minggli authored and jreback committed Mar 22, 2018
1 parent 34ba548 commit 5cf9773
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 59 deletions.
30 changes: 10 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,18 @@ def test_iso_constructor_raises(fmt):
Timedelta(fmt)


def test_td_constructor_on_nanoseconds():
@pytest.mark.parametrize('constructed_td, conversion', [
(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')
58 changes: 19 additions & 39 deletions pandas/tests/scalar/timedelta/test_formats.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,28 @@
# -*- 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

from pandas import Timedelta

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
@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

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

@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
td = Timedelta(microseconds=10)
result = td.isoformat()
expected = 'P0DT0H0M0.00001S'
assert result == expected

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

(Timedelta(milliseconds=1), 'P0DT0H0M0.001S'),
# don't strip every 0
result = Timedelta(minutes=1).isoformat()
expected = 'P0DT0H1M0S'
assert result == expected
(Timedelta(minutes=1), 'P0DT0H1M0S')])
def test_isoformat(td, expected_iso):
assert td.isoformat() == expected_iso

0 comments on commit 5cf9773

Please sign in to comment.