1

Continuing on my previous question, I have another dataframe that the week column starts with 201553. When I change weekly columns to timestamp, belwo occured. Please take a look.

a = pd.Int64Index([201553, 201601, 201602, 201603, 201604, 201605, 201606, 201607,
    201608, 201609])

print (pd.to_datetime(a.astype(str) + '1', format='%Y%W%w'))

DatetimeIndex(['2016-01-04', '2016-01-04', '2016-01-11', '2016-01-18', '2016-01-25',
           '2016-02-01', '2016-02-08', '2016-02-15', '2016-02-22',
           '2016-02-29'],
          dtype='datetime64[ns]', freq=None)

Notice that my first index starts with 201553 and when I change into DatatimeIndex, it shows '2016-01-04' which is wrong. I want need to have '2015-12-28' for the first index. Is there a way to change only the first index of a? Please let me know. Thanks!

-------edit------- 201553 means year 2015, 53rd week. If you see the DatetimeIndex, each date is Monday of each week. So week number 53 in year 2015 should start with '2015-12-28', but my DatatimeIndex is showing '2016-01-04'. All I need to do is fix this. Hope this makes more sense to everyone. Sorry for the confusion. Thanks!

-------edit2------ I kinda self-solved the question. I changed the a.index[0] to 201552 and it gave me the exact date I want. But still, I want to find out correct way to solve this problem. Thanks

Community
  • 1
  • 1
EJ Kang
  • 455
  • 2
  • 5
  • 17
  • 1
    If you want `2015011` to correspond to the first Monday in 2015 (i.e., 2015-01-05), then `2015521` will correspond to 2015-12-28. Look at `[datetime.datetime.strptime('2015-{:02d}-1'.format(i), '%Y-%W-%w') for i in range(54)]`. If you want `2015531` to correspond to 2015-12-28 then you are parsing something nonstandard other than [ISO weeks](https://en.wikipedia.org/wiki/ISO_week_date), and it looks like it would force `2015011` to correspond to 2014-12-29 -- a bizarre system. – unutbu Jan 16 '18 at 02:20
  • 201553 means year 2015, 53rd week. If you see the DatetimeIndex, each date is Monday of each week. So week number 53 in year 2015 should start with '2015-12-28', but my DatatimeIndex is showing '2016-01-04'. All I need to do is fix this. Is this making sense? Thanks – EJ Kang Jan 16 '18 at 02:30
  • There are only 52 weeks in 2015. `2015-12-28` is the 52nd Monday in 2015. `201553` and `201601` are two ways of referring to the same week. – unutbu Jan 16 '18 at 12:51
  • Oh that was the problem. All the time I thought 53 was the last week. Thank! – EJ Kang Jan 17 '18 at 01:33

0 Answers0