如果pandas数据帧低于1900,则更改一年

时间:2016-07-14 17:55:10

标签: python datetime pandas

我必须处理数据,其中某人使用的日期值为1700,而没有实际的事件日期。 1700打破了日期,从1900年开始,但我相信大家都知道。

我已将数据转换为datetime,然后尝试了if语句:

df["DATE"] = pd.to_datetime(df["DATE"])    
if df['DATE'].dt.year.any() < 1900
    #assigning today's date
    df['DATE'] = dt.datetime.today().strftime("%m/%d/%y")
else:
    #the original date value, formatted
    df["DATE"] = df["DATE"].map(lambda x: x.strftime("%m/%d/%y"))

if语句没有捕获1700,我收到错误:
 "ValueError: year=1700 is before 1900"

熊猫版:0.18.0 numpy版本:1.11.1

1 个答案:

答案 0 :(得分:2)

我很难完全复制这个问题,但你试过了吗?

with a as (
         -- your data example
         select '02400' HQ1,'02430' HQ2 from dual union 
         select '02430', '02465' HQ2 from dual union 
         select '02430', '02466' HQ2 from dual union 
         select '02465', '02475' HQ2 from dual union 
         select '02466', '02478' HQ2 from dual

         -- another data example in same table
         union 
         select '02461', '02479' HQ2 from dual
         union 
         select '02479', '02478' HQ2 from dual
         union 
         select '02461', '0247X' HQ2 from dual
         )

select a.HQ2, level tree_level
  from a
 start with HQ1 = '02400' 
CONNECT BY PRIOR HQ2 = HQ1