Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
773 views
in Technique[技术] by (71.8m points)

python - Convert Julian dates to normal dates in a dataframe?

I have a date column in a pandas DF with Julian dates. How can I convert these Julian dates to mm-dd-yyyy format.

Sample data

     ORG   CHAIN_NBR  SEQ_NBR     INT_STATUS  BLOCK_CODE_1  DATA_BLOCK_CODE_1
0   523         1        0          A             C             2012183
1   523         2        1          I             A             2013025
2   521         3        1          A             H             2007067
3   513         4        1          D             H             2001046
4   513         5        1          8             I             2006075

I was using jd2gcal function but it's not working. I was also trying to write a code like this but of no use.

for i,row in amna.iterrows():
    amna['DATE_BLOCK_CODE_1'] = datetime.datetime.strptime(row['DATE_BLOCK_CODE_1'], '%Y%j')

desired Output:

    ORG   CHAIN_NBR  SEQ_NBR     INT_STATUS  BLOCK_CODE_1  DATA_BLOCK_CODE_1
0   523         1        0          A             C             mm-dd-yyyy
1   523         2        1          I             A             mm-dd-yyyy
2   521         3        1          A             H             mm-dd-yyyy
3   513         4        1          D             H             mm-dd-yyyy
4   513         5        1          8             I             mm-dd-yyyy

Please help me with this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Let's try something like this

df['New Date'] = (pd.to_datetime((df.DATA_BLOCK_CODE_1 // 1000).astype(str)) + 
                 pd.to_timedelta(df.DATA_BLOCK_CODE_1 % 1000, unit='D'))

print(df)

   ORG  CHAIN_NBR  SEQ_NBR INT_STATUS BLOCK_CODE_1  DATA_BLOCK_CODE_1   New Date
0  523          1        0          A            C            2012183 2012-07-02
1  523          2        1          I            A            2013025 2013-01-26
2  521          3        1          A            H            2007067 2007-03-09
3  513          4        1          D            H            2001046 2001-02-16
4  513          5        1          8            I            2006075 2006-03-17

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...