def test_write_csv(): df = pd.DataFrame({ CommonFields.DATE: pd.to_datetime(["2020-04-01", "2020-04-02"]), CommonFields.FIPS: ["06045", "45123"], CommonFields.CASES: [234, 456], }) ts = TimeseriesDataset(df) expected_csv = """,,summary,summary,summary,summary,summary,summary,summary,summary,summary,value,value date,,has_value,min_date,max_date,max_value,min_value,latest_value,num_observations,largest_delta,largest_delta_date,2020-04-01 00:00:00,2020-04-02 00:00:00 fips,variable,,,,,,,,,,, 06045,cases,True,2020-04-01,2020-04-01,234,234,234,1,,,234, 45123,cases,True,2020-04-02,2020-04-02,456,456,456,1,,,,456 """ # Call common_df.write_csv with index set to ["fips", "date"], the expected normal index. with temppathlib.NamedTemporaryFile("w+") as tmp: wide_dates_df.write_csv(ts.get_date_columns(), tmp.path) assert expected_csv == tmp.file.read()
def test_wide_dates(): input_df = read_csv_and_index_fips_date( "fips,county,aggregate_level,date,m1,m2\n" "97111,Bar County,county,2020-04-01,1,\n" "97111,Bar County,county,2020-04-02,2,\n" "97222,Foo County,county,2020-04-01,,10\n" "97222,Foo County,county,2020-04-03,3,30\n") provenance = provenance_wide_metrics_to_series( read_csv_and_index_fips_date("fips,date,m1,m2\n" "97111,2020-04-01,src11,\n" "97111,2020-04-02,src11,\n" "97222,2020-04-01,,src22\n" "97222,2020-04-03,src21,src22\n"), structlog.get_logger(), ) ts = TimeseriesDataset(input_df.reset_index(), provenance=provenance) date_columns = ts.get_date_columns() assert to_dict(["fips", "variable"], date_columns["value"]) == { ("97111", "m1"): { pd.to_datetime("2020-04-01"): 1.0, pd.to_datetime("2020-04-02"): 2.0 }, ("97222", "m1"): { pd.to_datetime("2020-04-03"): 3.0 }, ("97222", "m2"): { pd.to_datetime("2020-04-01"): 10.0, pd.to_datetime("2020-04-03"): 30.0 }, } assert to_dict(["fips", "variable"], date_columns["provenance"]) == { ("97111", "m1"): { "value": "src11" }, ("97222", "m1"): { "value": "src21" }, ("97222", "m2"): { "value": "src22" }, }