pythonpandastimestampdatetimeindex

How to convert a pandas DatetimeIndex to Array of Timestamps?


I've been digging at this, but think I've confused myself on the various ways pandas can represent dates and times.

I've imported a csv of data which includes columnds for year, month, day, etc, and then converted that to a datetime column and then set it as an index - all good.

# import and name columns
epwNames = ['year','month','day','hour','minute','Datasource','DryBulb {C}','DewPoint {C}','RelHum {%}','Atmos Pressure {Pa}','ExtHorzRad {Wh/m2}','ExtDirRad {Wh/m2}','HorzIRSky {Wh/m2}','GloHorzRad {Wh/m2}','DirNormRad {Wh/m2}','DifHorzRad {Wh/m2}','GloHorzIllum {lux}','DirNormIllum {lux}','DifHorzIllum {lux}','ZenLum {Cd/m2}','WindDir {deg}','WindSpd {m/s}','TotSkyCvr {.1}','OpaqSkyCvr {.1}','Visibility {km}','Ceiling Hgt {m}','PresWeathObs','PresWeathCodes','Precip Wtr {mm}','Aerosol Opt Depth {.001}','SnowDepth {cm}','Days Last Snow','Albedo {.01}','Rain {mm}','Rain Quantity {hr}']
Weather = pd.read_csv(filepath,header=None,skiprows=8,names=epwNames)

# Format timestamp index
Weather['Datetime'] = pd.to_datetime(Weather[['year','month','day','hour']])
Weather.index = Weather['Datetime']

I have another function which uses the datetime, but is currently set up to require an array of Timestamps - this may or not be the best way to do it, but for example I have things like this, where 'timestamp' is the array being passed in:

get_julianDate = np.vectorize(pd.Timestamp.to_julian_date)
julianDay = get_julianDate(timestamp)

If I run it by passing in the DateTimeIndex, I get an attribute error that 'numpy.datetime64' object has no attribute 'year', which seems weird. Everything works fine if I pass through an array of Timestamps though.

I've tried some simple convertions, like just passing the DateTimeIndex into pd.Timestamp, but I guess that would have been too east. Is there a simple way to do this?


Solution

  • Two ways might work:

    get_julianDate(timestamp.to_list())
    # or use pd.Index.map directly:
    timestamp.map(pd.Timestamp.to_julian_date)