Pandas 将整型转换为日期不正确

Pandas 将整型转换为日期不正确,pandas,dataframe,jupyter-notebook,Pandas,Dataframe,Jupyter Notebook,我有一个有两列的数据集 df = pd.DataFrame({'Date': [195101, 195102, 195103, 195104, 195105], 'Value': [1.5, 0.9, -0.1, -0.3, -0.7]}) Date Value 0 195101 1.5 1 195102 0.9 2 195103 -0.1 3 195104 -0.3 4 195105 -0.7 检查类型后 d

我有一个有两列的数据集

df = pd.DataFrame({'Date': [195101, 195102, 195103, 195104, 195105],
                   'Value': [1.5, 0.9, -0.1, -0.3, -0.7]})
     Date  Value
0  195101    1.5
1  195102    0.9
2  195103   -0.1
3  195104   -0.3
4  195105   -0.7
检查类型后

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   Date    5 non-null      int64  
 1   Value   5 non-null      float64
dtypes: float64(1), int64(1)
memory usage: 208.0 bytes
结果是:

                           Date  Value
0 1970-01-01 00:00:00.000195101    1.5
1 1970-01-01 00:00:00.000195102    0.9
2 1970-01-01 00:00:00.000195103   -0.1
3 1970-01-01 00:00:00.000195104   -0.3
4 1970-01-01 00:00:00.000195105   -0.7
相反,我想得到年-月格式

      Date  Value
0  1951-01    1.5
1  1951-02    0.9
2  1951-03   -0.1
3  1951-04   -0.3
4  1951-05   -0.7
根据以下答案(已接受)解决问题,包括:

日期列是数字类型,因此默认情况下
pandas
认为它是自POSIX原点(1970-01-01)以来的纳秒数。要获得所需的内容,必须转换为字符串,然后提供正确的格式

# To properly format your numeric-dates:
pd.to_datetime(df.Date.astype(str), format='%Y%m')
#0   1951-01-01
#1   1951-02-01
#2   1951-03-01
#3   1951-04-01

如果您想要每月周期,则添加一个
.to\u周期

pd.to_datetime(df.Date.astype(str), format='%Y%m').dt.to_period('M')
#0    1951-01
#1    1951-02
#2    1951-03
#3    1951-04
# To properly format your numeric-dates:
pd.to_datetime(df.Date.astype(str), format='%Y%m')
#0   1951-01-01
#1   1951-02-01
#2   1951-03-01
#3   1951-04-01
pd.to_datetime(df.Date.astype(str), format='%Y%m').dt.to_period('M')
#0    1951-01
#1    1951-02
#2    1951-03
#3    1951-04