为什么我不能从给定的日期中成功提取月份?

时间:2019-08-21 22:22:19

标签: python pandas datetime

我在尝试从下表中的给定日期提取MONTH时遇到问题:

Out[65]: 
            PROD_NBR                             PROD_DESC  MAJOR_CAT_CD  \
0      -7.358821e+10                 VAL BABYS 1ST GENERAL          9687   
1      -7.358821e+10                 VAL BABYS 1ST GENERAL          9687   
2       7.358820e+10                FLAT WRAP BDAY GENERAL          9687   
3       7.358821e+10            BDAY MOTHER-IN-LAW GENERAL          9687   
4       7.358821e+10       EAS GRT-GRFATH/GRPA GRT-GRANDPA          9687   
              ...                                   ...           ...   
878509  8.770156e+09                GNP TUSSIN DM EXP 4 OZ          5228   
878510  3.458024e+10  PSEUDOEPHEDRINE 30MG TABLET 24CT PER          5228   
878511  3.458024e+10  PSEUDOEPHEDRINE 30MG TABLET 24CT PER          5228   
878512  3.458024e+10  PSEUDOEPHEDRINE 30MG TABLET 24CT PER          5228   
878513  3.638240e+10       MUCINEX EXPECTORANT TABLET 20CT          5228   
                            BSKT_ID           PHRMCY_NBR  SLS_DTE_NBR  \
0       600010665100006106120160128   748613589991092598     20160128   
1       600010665100006202720160208   748613589991092598     20160208   
2       600010665100007106220160518   748613589991092598     20160518   
3       600010665100006487120160310   748613589991092598     20160310   
4       600010665100006626120160324   748613589991092598     20160324   
                             ...                  ...          ...   
878509     400071264319000620160125  4754955394504903019     20160125   
878510     400071264319607020160318  4754955394504903019     20160318   
878511     400071264319336120160223  4754955394504903019     20160223   
878512     400071264319229120160213  4754955394504903019     20160213   
878513     400071264319109920160202  4754955394504903019     20160202   
        EXT_SLS_AMT  SLS_QTY  MAJOR_CAT_DESC         PHRMCY_NAM ST_CD  \
0              1.25        1  GREETING CARDS  GNP PHARMACY #860    NY   
1              1.25        1  GREETING CARDS  GNP PHARMACY #860    NY   
2              1.25        1  GREETING CARDS  GNP PHARMACY #860    NY   
3              1.75        1  GREETING CARDS  GNP PHARMACY #860    NY   
4              1.38        1  GREETING CARDS  GNP PHARMACY #860    NY   
             ...      ...             ...                ...   ...   
878509         3.99        1     HEALTH CARE  GNP PHARMACY #500    CT   
878510         2.94        1     HEALTH CARE  GNP PHARMACY #500    CT   
878511         2.94        1     HEALTH CARE  GNP PHARMACY #500    CT   
878512         2.94        1     HEALTH CARE  GNP PHARMACY #500    CT   
878513        14.99        1     HEALTH CARE  GNP PHARMACY #500    CT   
                                date  month  
0      1970-01-01 00:00:00.020160128      1  
1      1970-01-01 00:00:00.020160208      1  
2      1970-01-01 00:00:00.020160518      1  
3      1970-01-01 00:00:00.020160310      1  
4      1970-01-01 00:00:00.020160324      1  
                              ...    ...  
878509 1970-01-01 00:00:00.020160125      1  
878510 1970-01-01 00:00:00.020160318      1  
878511 1970-01-01 00:00:00.020160223      1  
878512 1970-01-01 00:00:00.020160213      1  
878513 1970-01-01 00:00:00.020160202      1

我尝试提取月份:

pharmtotal_df['date'] = pd.to_datetime(pharmtotal_df['SLS_DTE_NBR'])
pharmtotal_df['month'] = pharmtotal_df['date'].dt.month

我不确定为什么新的日期和月份列看起来如此混乱。有人知道我要去哪里了吗?

1 个答案:

答案 0 :(得分:1)

将列转换为字符串。

pharmtotal_df['date'] = pd.to_datetime(pharmtotal_df['SLS_DTE_NBR'].astype('str'))
pharmtotal_df['month'] = pharmtotal_df['date'].dt.month