def test_difference_nanoseconds_null(): assert_result_equals( render( make_table( make_column("A", [123, None, None], pa.timestamp("ns")), make_column("B", [None, 123, None], pa.timestamp("ns")), ), P( operation="difference", colname1="A", colname2="B", unit="nanosecond", outcolname="C", ), ), ArrowRenderResult( make_table( make_column("A", [123, None, None], pa.timestamp("ns")), make_column("B", [None, 123, None], pa.timestamp("ns")), make_column("C", [None, None, None], pa.int64(), format="{:,d}"), ), ), )
def test_difference_nulls(): assert_result_equals( render( make_table( make_column("A", [None, dt(2020, 3, 2), None]), make_column("B", [dt(2020, 1, 1), None, None]), ), P( operation="difference", colname1="A", colname2="B", unit="day", outcolname="C", ), ), ArrowRenderResult( make_table( make_column("A", [None, dt(2020, 3, 2), None]), make_column("B", [dt(2020, 1, 1), None, None]), make_column("C", [None, None, None], pa.float64()), ), ), )
def test_difference_seconds(): assert_result_equals( render( make_table( make_column("A", [dt(2019, 1, 1, 12, 1, 1), dt(2020, 3, 2, 8, 1, 1)]), make_column("B", [dt(2020, 1, 1, 2, 3, 4), dt(2020, 1, 2, 1, 2, 3)]), ), P( operation="difference", colname1="A", colname2="B", unit="second", outcolname="C", ), ), ArrowRenderResult( make_table( make_column("A", [dt(2019, 1, 1, 12, 1, 1), dt(2020, 3, 2, 8, 1, 1)]), make_column("B", [dt(2020, 1, 1, 2, 3, 4), dt(2020, 1, 2, 1, 2, 3)]), make_column("C", [31500123.0, -5209138.0]), ), ), )
def test_difference_fractional(): assert_result_equals( render( make_table( make_column("A", [dt(2019, 1, 1, 0), dt(2020, 3, 2, 0, 0)]), make_column("B", [dt(2020, 1, 1, 12), dt(2020, 1, 2, 0, 45)]), ), P( operation="difference", colname1="A", colname2="B", unit="day", outcolname="C", ), ), ArrowRenderResult( make_table( make_column("A", [dt(2019, 1, 1, 0), dt(2020, 3, 2, 0, 0)]), make_column("B", [dt(2020, 1, 1, 12), dt(2020, 1, 2, 0, 45)]), make_column("C", [365.5, -59.96875]), ), ), )
def test_difference_days(): assert_result_equals( render( make_table( make_column("A", [dt(2019, 1, 1), dt(2020, 3, 2)]), make_column("B", [dt(2020, 1, 1), dt(2020, 1, 2)]), ), P( operation="difference", colname1="A", colname2="B", unit="day", outcolname="C", ), ), ArrowRenderResult( make_table( make_column("A", [dt(2019, 1, 1), dt(2020, 3, 2)]), make_column("B", [dt(2020, 1, 1), dt(2020, 1, 2)]), make_column("C", [365.0, -60.0]), ), ), )
def test_quickfix_convert_value_strings_to_numbers(): assert_result_equals( render( make_table( make_column("A", [1, 1, 1]), make_column("B", ["a", "b", "a"]), make_column("C", ["a", "b", "a"]), ), P( groups=dict(colnames=["A"], group_dates=False, date_granularities={}), aggregations=[ dict(operation="mean", colname="B", outname="mean"), dict(operation="sum", colname="C", outname="sum"), ], ), ), ArrowRenderResult( make_table(), [ RenderError( i18n_message( "non_numeric_colnames.error", {"n_columns": 2, "first_colname": "B"}, ), quick_fixes=[ QuickFix( i18n_message("non_numeric_colnames.quick_fix.text"), QuickFixAction.PrependStep( "converttexttonumber", {"colnames": ["B", "C"]} ), ) ], ) ], ), )
def test_convert_numbers_all_null(): assert_result_equals( render(make_table(make_column("A", [None], pa.float64())), P(colnames=["A"])), ArrowRenderResult(make_table(make_column("A", [None], pa.utf8()))), )
def test_convert_str_does_nothing(): assert_result_equals( render(make_table(make_column("A", ["x"])), P(colnames=["A"])), ArrowRenderResult(make_table(make_column("A", ["x"]))), )
def test_default_params_does_nothing(): assert_result_equals( render(make_table(make_column("A", [1, 2, 3])), P()), ArrowRenderResult(make_table(make_column("A", [1, 2, 3]))), )
def test_assert_result_equals_check_json(): with pytest.raises(AssertionError, match=r"-\{'foo': 'bar'\}\n\+\{\}"): assert_result_equals( ArrowRenderResult(make_table()), ArrowRenderResult(make_table(), json={"foo": "bar"}), )
def test_render_timezone_positive_offset(): assert_result_equals( render( make_table( make_column( "A", [ dt(2000, 1, 1), dt(2000, 1, 1, 4, 59), # DST starts 2am Apr 2, 2000 -- and becomes 3am dt(2000, 4, 2, 5), dt(2000, 4, 2, 6), dt(2000, 4, 2, 7), # DST ends 2am Oct 29, 2000 -- and becomes 1am dt(2000, 10, 29, 5), dt(2000, 10, 29, 6), dt(2000, 10, 29, 7), ], )), P( colname="A", timezone="America/Montreal", outputs=[ dict(outcolname="date", part="date"), dict(outcolname="time_minutes", part="time_minutes"), dict(outcolname="year", part="year"), ], ), ), ArrowRenderResult( make_table( make_column( "date", [ datetime.date(1999, 12, 31), datetime.date(1999, 12, 31), datetime.date(2000, 4, 2), datetime.date(2000, 4, 2), datetime.date(2000, 4, 2), datetime.date(2000, 10, 29), datetime.date(2000, 10, 29), datetime.date(2000, 10, 29), ], ), make_column( "time_minutes", [ "19:00", "23:59", "00:00", "01:00", "03:00", "01:00", "01:00", "02:00", ], ), make_column( "year", [1999, 1999, 2000, 2000, 2000, 2000, 2000, 2000], pa.int16(), format="{:d}", ), )), )