0

I am using pd.Grouper to group my time-series in frequency of 3 days.To retrieve the time array, I use date = df.index.values which returns me an array of time which looks like this:

array(['2010-01-31T00:00:00.000000000', '2010-02-03T00:00:00.000000000',
   '2017-05-12T00:00:00.000000000', '2017-05-15T00:00:00.000000000',
   '2017-05-18T00:00:00.000000000', '2017-05-21T00:00:00.000000000',
   '2017-05-24T00:00:00.000000000', '2017-05-27T00:00:00.000000000',
   '2017-05-30T00:00:00.000000000', '2017-06-02T00:00:00.000000000',
   '2017-06-05T00:00:00.000000000', '2017-06-08T00:00:00.000000000',
   '2017-06-11T00:00:00.000000000', '2017-06-14T00:00:00.000000000',
   '2017-06-17T00:00:00.000000000', '2017-06-20T00:00:00.000000000',
   '2017-06-23T00:00:00.000000000', '2017-06-26T00:00:00.000000000',
   '2017-06-29T00:00:00.000000000', '2017-07-02T00:00:00.000000000',
   '2017-07-05T00:00:00.000000000', '2017-07-08T00:00:00.000000000',
   '2017-07-11T00:00:00.000000000', '2017-07-14T00:00:00.000000000',
   '2017-07-17T00:00:00.000000000', '2017-07-20T00:00:00.000000000',
   '2017-07-23T00:00:00.000000000', '2017-07-26T00:00:00.000000000',
   '2017-07-29T00:00:00.000000000', '2017-08-01T00:00:00.000000000',
   '2017-08-04T00:00:00.000000000', '2017-08-07T00:00:00.000000000'],
  dtype='datetime64[ns]')

I have been trying to get just date (and eventually MJD out of it). It works when I copy 1-2 elements of this array and do this;

times =['2010-02-03T00:00:00.000000000','2010-02-03T00:00:00.000000000']
t = Time(times, format='isot', scale='utc') 
print(t.mjd)
>>[55230. 55230.]

However, I am not able to use the same type of code for the entire array

from astropy.time import Time
t = Time(date, format='isot', scale='utc') 
print(t.mjd)

it gives me an error "Input values did not match the format class isot". So, I guessed that Time will require list rather than an array but changing Date to list doesn't fix the problem. I am not able to work it out, the example above is a list of 2 strings and it works fine. What am I doing wrong here? I have tried few other ways using pandas and trying to loop over elements. Thanks for the help. Abhi

Abhishek
  • 49
  • 1
  • 7

2 Answers2

1

Since astropy 3.1 there is built-in support for datetime64, so you can simply do this:

In [2]: dates = np.array(['2010-01-31T00:00:00', '2010-02-03T00:00:00'],
   ...:   dtype='datetime64[ns]')
   ...:   

In [3]: tm = Time(dates)

In [4]: tm.mjd
Out[4]: array([55227., 55230.])
Tom Aldcroft
  • 2,339
  • 1
  • 14
  • 16
-1

Found a way to do this, after looking at this link

from astropy.time import Time
date = df.index.values
a= []
for i in [x for x in date]:
    ts = pd.to_datetime(str(i)) 
    d = ts.strftime('%Y-%m-%d')
    a.append(d)
    print(d)

grouped_date = Time(a, format='iso', out_subfmt='date')
grouped_date_mjd = grouped_date.mjd

print(a[0:3], grouped_date_mjd[0:3])
>> ['2010-01-31', '2010-02-03', '2010-02-06'] [55227. 55230. 55233.]
Abhishek
  • 49
  • 1
  • 7