programing

Python 오류: TypeError: 'Timestamp' 형식의 개체가 JSON 직렬화 가능이 아닙니다.

iphone6s 2023. 3. 13. 20:14
반응형

Python 오류: TypeError: 'Timestamp' 형식의 개체가 JSON 직렬화 가능이 아닙니다.

datetime64 [ ns ]타입의 타임스탬프 컬럼이 있는 데이터 프레임이 있습니다.Salesforce 플랫폼에 삽입하려고 하면 'TypeError: Timestamp' 유형의 개체가 JSON 직렬화 가능하지 않습니다'라는 오류가 나타납니다.이 타임스탬프 열을 변경하여 올바르게 업데이트하려면 어떻게 해야 합니까?아래는 데이터 프레임의 그림입니다.

Id,Name,Date,Type
1,ProdA,2018-05-18 04:45:08,S
1,ProdB,2018-05-18 02:15:00,S
1,ProdC,2018-05-16 10:20:00,S

다음 4개 열의 데이터 유형:

Id                                     object
Name                                   object
Date                           datetime64[ns]
Type                                   object
dtype: object

아무나 도와주실 수 있나요?감사해요.

datetime을 문자열로 변환할 수 있습니다.

df['Date'] = df['Date'].astype(str)

또는 다음 중 하나를 선택합니다.

df['Date'] = df['Date'].dt.strftime('%Y-%m-%d %H:%M:%S')

print (df.dtypes)
Id      object
Name    object
Date    object
Type    object
dtype: object

에러가 발생했을 경우TimeStamp has no attribute as "astype(str)"예를 들어 다음과 같이 시도할 수 있습니다.str(timeseries.index[0])타임스탬프를 문자열로 변환하여 시리얼화할 수 있습니다.

언급URL : https://stackoverflow.com/questions/50404559/python-error-typeerror-object-of-type-timestamp-is-not-json-serializable

반응형