TypeError: Object of type 'time' is not JSON serializable

TypeError: Object of type 'time' is not JSON serializable

我的数据集如下所示。使用 altair 进行可视化。我正在尝试绘制时间序列图,以便我的时间在 X 轴上,拾音器在 y 轴上,颜色代表星期几。

我的可视化代码如下,但是,每当我尝试绘图时,我都会收到错误提示

Object of type "time" is not JSON serializable.

我试着关注 jgbarah 在 here 中提到的评论。不幸的是我无法解决它。

{
  'day_of_week': {
    0: 'Friday',
    1: 'Friday',
    2: 'Friday',
    3: 'Friday',
    4: 'Friday',
    5: 'Friday',
    6: 'Friday',
    7: 'Friday',
    8: 'Friday',
    9: 'Friday',
    10: 'Friday',
    11: 'Friday',
    12: 'Friday',
    13: 'Friday',
    14: 'Friday',
    15: 'Friday',
    16: 'Friday',
    17: 'Friday',
    18: 'Friday',
    19: 'Friday',
    20: 'Friday',
    21: 'Friday',
    22: 'Friday',
    23: 'Friday',
    24: 'Monday',
    25: 'Monday',
    26: 'Monday',
    27: 'Monday',
    28: 'Monday',
    29: 'Monday',
    30: 'Monday',
    31: 'Monday',
    32: 'Monday',
    33: 'Monday',
    34: 'Monday',
    35: 'Monday',
    36: 'Monday',
    37: 'Monday',
    38: 'Monday',
    39: 'Monday',
    40: 'Monday',
    41: 'Monday',
    42: 'Monday',
    43: 'Monday',
    44: 'Monday',
    45: 'Monday',
    46: 'Monday',
    47: 'Monday',
    48: 'Saturday',
    49: 'Saturday',
    50: 'Saturday',
    51: 'Saturday',
    52: 'Saturday',
    53: 'Saturday',
    54: 'Saturday',
    55: 'Saturday',
    56: 'Saturday',
    57: 'Saturday',
    58: 'Saturday',
    59: 'Saturday',
    60: 'Saturday',
    61: 'Saturday',
    62: 'Saturday',
    63: 'Saturday',
    64: 'Saturday',
    65: 'Saturday',
    66: 'Saturday',
    67: 'Saturday',
    68: 'Saturday',
    69: 'Saturday',
    70: 'Saturday',
    71: 'Saturday',
    72: 'Sunday',
    73: 'Sunday',
    74: 'Sunday',
    75: 'Sunday',
    76: 'Sunday',
    77: 'Sunday',
    78: 'Sunday',
    79: 'Sunday',
    80: 'Sunday',
    81: 'Sunday',
    82: 'Sunday',
    83: 'Sunday',
    84: 'Sunday',
    85: 'Sunday',
    86: 'Sunday',
    87: 'Sunday',
    88: 'Sunday',
    89: 'Sunday',
    90: 'Sunday',
    91: 'Sunday',
    92: 'Sunday',
    93: 'Sunday',
    94: 'Sunday',
    95: 'Sunday',
    96: 'Thursday',
    97: 'Thursday',
    98: 'Thursday',
    99: 'Thursday',
    100: 'Thursday',
    101: 'Thursday',
    102: 'Thursday',
    103: 'Thursday',
    104: 'Thursday',
    105: 'Thursday',
    106: 'Thursday',
    107: 'Thursday',
    108: 'Thursday',
    109: 'Thursday',
    110: 'Thursday',
    111: 'Thursday',
    112: 'Thursday',
    113: 'Thursday',
    114: 'Thursday',
    115: 'Thursday',
    116: 'Thursday',
    117: 'Thursday',
    118: 'Thursday',
    119: 'Thursday',
    120: 'Tuesday',
    121: 'Tuesday',
    122: 'Tuesday',
    123: 'Tuesday',
    124: 'Tuesday',
    125: 'Tuesday',
    126: 'Tuesday',
    127: 'Tuesday',
    128: 'Tuesday',
    129: 'Tuesday',
    130: 'Tuesday',
    131: 'Tuesday',
    132: 'Tuesday',
    133: 'Tuesday',
    134: 'Tuesday',
    135: 'Tuesday',
    136: 'Tuesday',
    137: 'Tuesday',
    138: 'Tuesday',
    139: 'Tuesday',
    140: 'Tuesday',
    141: 'Tuesday',
    142: 'Tuesday',
    143: 'Tuesday',
    144: 'Wedsnesday',
    145: 'Wedsnesday',
    146: 'Wedsnesday',
    147: 'Wedsnesday',
    148: 'Wedsnesday',
    149: 'Wedsnesday',
    150: 'Wedsnesday',
    151: 'Wedsnesday',
    152: 'Wedsnesday',
    153: 'Wedsnesday',
    154: 'Wedsnesday',
    155: 'Wedsnesday',
    156: 'Wedsnesday',
    157: 'Wedsnesday',
    158: 'Wedsnesday',
    159: 'Wedsnesday',
    160: 'Wedsnesday',
    161: 'Wedsnesday',
    162: 'Wedsnesday',
    163: 'Wedsnesday',
    164: 'Wedsnesday',
    165: 'Wedsnesday',
    166: 'Wedsnesday',
    167: 'Wedsnesday'
  },
  'time': {
    0: datetime.time(0, 0),
    1: datetime.time(1, 0),
    2: datetime.time(2, 0),
    3: datetime.time(3, 0),
    4: datetime.time(4, 0),
    5: datetime.time(5, 0),
    6: datetime.time(6, 0),
    7: datetime.time(7, 0),
    8: datetime.time(8, 0),
    9: datetime.time(9, 0),
    10: datetime.time(10, 0),
    11: datetime.time(11, 0),
    12: datetime.time(12, 0),
    13: datetime.time(13, 0),
    14: datetime.time(14, 0),
    15: datetime.time(15, 0),
    16: datetime.time(16, 0),
    17: datetime.time(17, 0),
    18: datetime.time(18, 0),
    19: datetime.time(19, 0),
    20: datetime.time(20, 0),
    21: datetime.time(21, 0),
    22: datetime.time(22, 0),
    23: datetime.time(23, 0),
    24: datetime.time(0, 0),
    25: datetime.time(1, 0),
    26: datetime.time(2, 0),
    27: datetime.time(3, 0),
    28: datetime.time(4, 0),
    29: datetime.time(5, 0),
    30: datetime.time(6, 0),
    31: datetime.time(7, 0),
    32: datetime.time(8, 0),
    33: datetime.time(9, 0),
    34: datetime.time(10, 0),
    35: datetime.time(11, 0),
    36: datetime.time(12, 0),
    37: datetime.time(13, 0),
    38: datetime.time(14, 0),
    39: datetime.time(15, 0),
    40: datetime.time(16, 0),
    41: datetime.time(17, 0),
    42: datetime.time(18, 0),
    43: datetime.time(19, 0),
    44: datetime.time(20, 0),
    45: datetime.time(21, 0),
    46: datetime.time(22, 0),
    47: datetime.time(23, 0),
    48: datetime.time(0, 0),
    49: datetime.time(1, 0),
    50: datetime.time(2, 0),
    51: datetime.time(3, 0),
    52: datetime.time(4, 0),
    53: datetime.time(5, 0),
    54: datetime.time(6, 0),
    55: datetime.time(7, 0),
    56: datetime.time(8, 0),
    57: datetime.time(9, 0),
    58: datetime.time(10, 0),
    59: datetime.time(11, 0),
    60: datetime.time(12, 0),
    61: datetime.time(13, 0),
    62: datetime.time(14, 0),
    63: datetime.time(15, 0),
    64: datetime.time(16, 0),
    65: datetime.time(17, 0),
    66: datetime.time(18, 0),
    67: datetime.time(19, 0),
    68: datetime.time(20, 0),
    69: datetime.time(21, 0),
    70: datetime.time(22, 0),
    71: datetime.time(23, 0),
    72: datetime.time(0, 0),
    73: datetime.time(1, 0),
    74: datetime.time(2, 0),
    75: datetime.time(3, 0),
    76: datetime.time(4, 0),
    77: datetime.time(5, 0),
    78: datetime.time(6, 0),
    79: datetime.time(7, 0),
    80: datetime.time(8, 0),
    81: datetime.time(9, 0),
    82: datetime.time(10, 0),
    83: datetime.time(11, 0),
    84: datetime.time(12, 0),
    85: datetime.time(13, 0),
    86: datetime.time(14, 0),
    87: datetime.time(15, 0),
    88: datetime.time(16, 0),
    89: datetime.time(17, 0),
    90: datetime.time(18, 0),
    91: datetime.time(19, 0),
    92: datetime.time(20, 0),
    93: datetime.time(21, 0),
    94: datetime.time(22, 0),
    95: datetime.time(23, 0),
    96: datetime.time(0, 0),
    97: datetime.time(1, 0),
    98: datetime.time(2, 0),
    99: datetime.time(3, 0),
    100: datetime.time(4, 0),
    101: datetime.time(5, 0),
    102: datetime.time(6, 0),
    103: datetime.time(7, 0),
    104: datetime.time(8, 0),
    105: datetime.time(9, 0),
    106: datetime.time(10, 0),
    107: datetime.time(11, 0),
    108: datetime.time(12, 0),
    109: datetime.time(13, 0),
    110: datetime.time(14, 0),
    111: datetime.time(15, 0),
    112: datetime.time(16, 0),
    113: datetime.time(17, 0),
    114: datetime.time(18, 0),
    115: datetime.time(19, 0),
    116: datetime.time(20, 0),
    117: datetime.time(21, 0),
    118: datetime.time(22, 0),
    119: datetime.time(23, 0),
    120: datetime.time(0, 0),
    121: datetime.time(1, 0),
    122: datetime.time(2, 0),
    123: datetime.time(3, 0),
    124: datetime.time(4, 0),
    125: datetime.time(5, 0),
    126: datetime.time(6, 0),
    127: datetime.time(7, 0),
    128: datetime.time(8, 0),
    129: datetime.time(9, 0),
    130: datetime.time(10, 0),
    131: datetime.time(11, 0),
    132: datetime.time(12, 0),
    133: datetime.time(13, 0),
    134: datetime.time(14, 0),
    135: datetime.time(15, 0),
    136: datetime.time(16, 0),
    137: datetime.time(17, 0),
    138: datetime.time(18, 0),
    139: datetime.time(19, 0),
    140: datetime.time(20, 0),
    141: datetime.time(21, 0),
    142: datetime.time(22, 0),
    143: datetime.time(23, 0),
    144: datetime.time(0, 0),
    145: datetime.time(1, 0),
    146: datetime.time(2, 0),
    147: datetime.time(3, 0),
    148: datetime.time(4, 0),
    149: datetime.time(5, 0),
    150: datetime.time(6, 0),
    151: datetime.time(7, 0),
    152: datetime.time(8, 0),
    153: datetime.time(9, 0),
    154: datetime.time(10, 0),
    155: datetime.time(11, 0),
    156: datetime.time(12, 0),
    157: datetime.time(13, 0),
    158: datetime.time(14, 0),
    159: datetime.time(15, 0),
    160: datetime.time(16, 0),
    161: datetime.time(17, 0),
    162: datetime.time(18, 0),
    163: datetime.time(19, 0),
    164: datetime.time(20, 0),
    165: datetime.time(21, 0),
    166: datetime.time(22, 0),
    167: datetime.time(23,
    0)
  },
  'pickups': {
    0: 1188.6,
    1: 691.6,
    2: 389.0,
    3: 256.2,
    4: 216.0,
    5: 270.0,
    6: 357.4,
    7: 540.0,
    8: 785.4,
    9: 958.2,
    10: 768.4,
    11: 697.2,
    12: 718.0,
    13: 710.4,
    14: 747.6,
    15: 834.6,
    16: 913.6,
    17: 1025.2,
    18: 1124.6,
    19: 1281.8,
    20: 1226.8,
    21: 1202.6,
    22: 1324.4,
    23: 1534.0,
    24: 585.2,
    25: 367.6,
    26: 185.2,
    27: 109.8,
    28: 142.4,
    29: 254.4,
    30: 368.8,
    31: 562.4,
    32: 780.0,
    33: 880.6,
    34: 695.0,
    35: 598.2,
    36: 596.2,
    37: 590.0,
    38: 634.0,
    39: 703.4,
    40: 735.2,
    41: 804.8,
    42: 935.0,
    43: 982.4,
    44: 902.0,
    45: 894.2,
    46: 941.2,
    47: 834.2,
    48: 1583.6,
    49: 1332.8,
    50: 989.6,
    51: 752.2,
    52: 609.4,
    53: 361.6,
    54: 256.2,
    55: 273.6,
    56: 353.8,
    57: 463.4,
    58: 606.6,
    59: 697.2,
    60: 835.6,
    61: 890.8,
    62: 883.0,
    63: 1047.8,
    64: 1395.2,
    65: 1675.6,
    66: 1783.4,
    67: 1918.0,
    68: 1980.6,
    69: 2063.0,
    70: 2155.8,
    71: 2102.0,
    72: 1329.2,
    73: 1133.2,
    74: 1040.6,
    75: 764.8,
    76: 585.6,
    77: 370.6,
    78: 222.6,
    79: 228.2,
    80: 275.8,
    81: 413.4,
    82: 552.8,
    83: 730.2,
    84: 814.4,
    85: 838.0,
    86: 860.8,
    87: 877.0,
    88: 874.6,
    89: 944.0,
    90: 1017.6,
    91: 971.6,
    92: 770.6,
    93: 826.4,
    94: 911.4,
    95: 849.4,
    96: 936.8,
    97: 486.0,
    98: 286.6,
    99: 179.6,
    100: 153.2,
    101: 216.4,
    102: 345.8,
    103: 581.2,
    104: 851.2,
    105: 984.8,
    106: 804.6,
    107: 680.0,
    108: 713.4,
    109: 676.2,
    110: 738.6,
    111: 829.4,
    112: 920.4,
    113: 1068.4,
    114: 1212.0,
    115: 1406.0,
    116: 1361.6,
    117: 1428.0,
    118: 1532.0,
    119: 1478.8,
    120: 579.2,
    121: 343.8,
    122: 186.0,
    123: 131.6,
    124: 112.6,
    125: 191.2,
    126: 323.0,
    127: 544.8,
    128: 829.6,
    129: 979.0,
    130: 856.6,
    131: 696.8,
    132: 716.8,
    133: 675.2,
    134: 745.2,
    135: 798.0,
    136: 855.4,
    137: 1002.4,
    138: 1418.0,
    139: 1723.6,
    140: 1433.8,
    141: 1193.8,
    142: 1196.6,
    143: 1018.6,
    144: 706.4,
    145: 435.8,
    146: 241.0,
    147: 151.6,
    148: 136.8,
    149: 206.8,
    150: 325.4,
    151: 548.0,
    152: 818.6,
    153: 953.0,
    154: 800.4,
    155: 661.8,
    156: 667.6,
    157: 676.2,
    158: 708.6,
    159: 785.0,
    160: 888.6,
    161: 992.2,
    162: 1185.0,
    163: 1303.0,
    164: 1176.8,
    165: 1197.8,
    166: 1308.2,
    167: 1255.2
  }
}

可视化代码:

alt.Chart(df).mark_rect().encode(
    alt.X('time:O'),
    alt.Y('pickups:Q'),
    alt.Color('day_of_week:O')
)

我是 python 的新手。请提供您的代码的解释。

JSON 只接受以下任何一种数据类型:

字符串、数字、对象(JSON对象)、数组、布尔值、null

如您所见,时间对象不是其中之一。

您需要使用 str(some_time_object) 函数将时间对象转换为字符串,然后更新 JSON 字符串。