【问题标题】:Add a date column in pandas df using constant value in str使用 str 中的常量值在 pandas df 中添加日期列
【发布时间】:2017-04-13 00:51:36
【问题描述】:

我在 pandas df 中有一张桌子

    product_id_x    product_id_y    count
0   2727846            7872456       1
1   29234              2932348       2
2   29346              9137500       1
3   29453              91365738      1
4   2933666            91323494      1

我想添加一个我在 str 中定义的新列“日期”。

dateSelect = "'2016-11-06'"

所以我添加了一个新的常量列

df['dates'] = dateSelect 

但我得到的结果是

   product_id_x   product_id_y    count   dates
0   2727846          7872456         1  '2016-11-06'
1   29234            2932348         2  '2016-11-06'
2   29346            9137500         1  '2016-11-06'
3   29453            91365738        1  '2016-11-06'
4   2933666          91323494        1  '2016-11-06'

日期中的值用引号括起来。和

type(df['dates']) = str

但我希望它采用日期格式,因为我还要将此表存储在我的 mysql 数据库中。我希望类型是日期。

from sqlalchemy import create_engine
engine = create_engine('mysql+mysqldb://name:pwd@xxx.xx.xx.x/dbname', echo=False)
df.to_sql(name='tablename', con=engine, if_exists = 'append', index=False)

【问题讨论】:

    标签: python python-2.7 python-3.x pandas


    【解决方案1】:

    我想你可以先用replace' by empty space 然后to_datetime:

    dateSelect = pd.to_datetime("'2016-11-06'".replace("'",""))
    print (dateSelect)
    2016-11-06 00:00:00
    
    print (type(dateSelect))
    <class 'pandas.tslib.Timestamp'>
    

    df['dates'] = pd.to_datetime("'2016-11-06'".replace("'",""))
    
    print (df)
       product_id_x  product_id_y  count      dates
    0       2727846       7872456      1 2016-11-06
    1         29234       2932348      2 2016-11-06
    2         29346       9137500      1 2016-11-06
    3         29453      91365738      1 2016-11-06
    4       2933666      91323494      1 2016-11-06
    
    print (df.dtypes)
    product_id_x             int64
    product_id_y             int64
    count                    int64
    dates           datetime64[ns]
    dtype: object
    

    【讨论】:

    • 是的,先生,在您回答前几秒钟我也尝试过,没有 .replace("'","") 也可以正常工作
    • 是的,如果有双 ""'' 一起,首先需要删除内部引号,然后它就完美了。或者只使用"2016-11-06"'2016-11-06' 之类的,则不需要replace
    【解决方案2】:

    最直接的路线

    df['dates'] = pd.Timestamp('2016-11-06')
    df
    
       product_id_x  product_id_y  count      dates
    0       2727846       7872456      1 2016-11-06
    1         29234       2932348      2 2016-11-06
    2         29346       9137500      1 2016-11-06
    3         29453      91365738      1 2016-11-06
    4       2933666      91323494      1 2016-11-06
    

    【讨论】:

      【解决方案3】:

      啊! @jezrael 最先到达那里...

       print timeit.timeit("""
      import pandas as pd
      import datetime as dt
      import timeit
      df = pd.read_csv('date_time_pandas.csv')
      dateSelect_str = "2016-11-06"
      
      # using standard datetime
      dateSelect = dt.datetime.strptime(dateSelect_str,"%Y-%m-%d")
      df['dates'] = dateSelect
      #print(df['dates'])
      """,number=100)
      
      
      # Alternate method using pandas datetime
      print timeit.timeit("""
      import pandas as pd
      import datetime as dt
      import timeit
      df = pd.read_csv('date_time_pandas.csv')
      dateSelect_str = "2016-11-06"
      
      dateSelect = pd.to_datetime(dateSelect_str, format='%Y-%m-%d', errors='ignore')
      df['dates'] = dateSelect
      #print df['dates']
      """,number=100)
      

      给出输出 -

      0.228258825751
      0.167258402887
      

      平均而言。

      结论在这种情况下使用pd_datetime效率更高

      【讨论】:

        【解决方案4】:

        不要在里面放双引号,避免将其定义为字符串。

        dateSelect = '2016-11-06'  
        df['dates'] = dateSelect 
        

        【讨论】:

          猜你喜欢
          • 2020-01-14
          • 2017-02-24
          • 1970-01-01
          • 1970-01-01
          • 2017-09-26
          • 2021-11-09
          • 2021-02-01
          • 1970-01-01
          • 2019-09-12
          相关资源
          最近更新 更多