1

I am trying to convert Julian codes to calendar dates in pandas using :

pd.to_datetime(43390, unit = 'D', origin = 'Julian')

This is giving me ValueError: origin Julian cannot be converted to a Timestamp

Isma
  • 14,604
  • 5
  • 37
  • 51
  • Is the 'date' 43390 from Excel? If so, these links might be useful: https://stackoverflow.com/questions/38454403/convert-excel-style-date-with-pandas and https://support.microsoft.com/en-us/office/date-systems-in-excel-e7fe7167-48a9-4b96-bb53-5612a800b487 – jsmart Aug 16 '20 at 07:40

3 Answers3

0

You need to set origin = 'julian'

pd.to_datetime(43390, unit = 'D', origin = 'julian')

but this number (43390) throws

OutOfBoundsDatetime: 43390 is Out of Bounds for origin='julian'

because the bounds are from 2333836 to 2547339
(Timestamp('1677-09-21 12:00:00') to Timestamp('2262-04-11 12:00:00'))

Ricardo Rendich
  • 666
  • 5
  • 6
0

Method 1 - using Julian for origin didn't work

Method 2 - using excel start date to calculate other dates. All other date values will be referenced from excel default start date.

Finally this worked for me.

pd.to_datetime(43390, unit = 'D', origin=pd.Timestamp("30-12-1899"))
0


Below code works only for 6 digit julian value. It also handles the calendar date for leap and non-leap years.

A Julian date is considered as "CYYDDD". Where C represents century, YY represents Year and DDD represents total days which are then further defined in Days and Months.

    import pandas as pd
    from datetime import datetime
    jul_date = '120075'
    add_days = int(jul_date[3:6])
    cal_date = pd.to_datetime(datetime.strptime(str(19+int(jul_date[0:1]))+jul_date[1:3]+'-01-01','%Y-%m-%d'))-timedelta(1)+pd.DateOffset(days= add_days)
    print(cal_date.strftime('%Y-%m-%d'))


output: 2020-03-15
without timedelta(1): 2020-03-16


Here datetime.strptime function is being used to cast date type from string to date.
%Y represents year in 4 digit (1980)
%m & %d represents month and day in digits.
strftime('%Y-%m-%d') is used to remove timestamp from the date.
timedelta(1) :- It's used to minus one day from the date because we've concatenated year with '01-01'. so when total no's of days being split to days and months, one day will not be extra.