0

Column 2 within my csv file looks like the following:

20150926T104044Z
20150926T104131Z

and so on.

I have a definition created that will change the listed date into a julian date, but was wondering how I can go about altering this specific column of data?

Is there a way I can make python change the dates within the csv to a their julian date equivalent? Can I split the column into two csv's and translate the julian date from there?

haramassive
  • 163
  • 1
  • 8

2 Answers2

0

You might be overthinking it. Try this.

from dateutil.parser import parse
import csv

def get_julian(_date):
    # _date is holding 20150926T104044Z
    the_date = parse(_date)
    julian_start = parse('19000101T000000Z')
    julian_days = (the_date - julian_start).days
    return julian_days

with open('filename.csv') as f:
    csv_reader = csv.reader(f)
    for row in csv_reader:
        # Column 2, right?
        row[1] = get_julian(row[1])

        # Do things and stuff with your corrected data.
UtahJarhead
  • 2,091
  • 1
  • 14
  • 21
0

I observed that there are many interpretations to Julian Day, One is Oridinal date(day of the year) and another one day from Monday, January 1, 4713 BC.

import pandas as pd
import datetime
import jdcal

df = pd.read_csv("path/to/your/csv")


def tojulianDate(date):
    return datetime.datetime.strptime(date, '%Y%m%dT%H%M%SZ').strftime('%y%j')
def tojulianDate2(date):
    curr_date = datetime.datetime.strptime(date, '%Y%m%dT%H%M%SZ')
    curr_date_tuple = curr_date.timetuple()
    return int(sum(jdcal.gcal2jd(curr_date_tuple.tm_year, curr_date_tuple.tm_mon, curr_date_tuple.tm_mday)))
df['Calendar_Dates'] = df['Calendar_Dates'].apply(tojulianDate2)

df.to_csv('path/to/modified/csv')

Method "toJulianDate" can be used to get the day of the year or Oridinal Date. for second format, there is a library called jdcal to convert gregorian date to julian day or vice versa which is done in toJulianDate2 . This can also be done directly by opening csv and without loading into a dataframe.

Similar question was answered here Extract day of year and Julian day from a string date in python

Satheesh K
  • 501
  • 1
  • 3
  • 16