0

I have a numpy array 'date1' of size 100 which stores dates like:

>>> date1[0]
datetime.datetime(2011, 1, 2, 0, 0)

date1 index 1 through 99 I haven't shown but have other dates stored in them.

But I get the following:
z1 = list(set(date1))
>>> z1[0]
datetime.datetime(2011, 5, 7, 0, 0)

Why does the set operation mess up the order of values stored in the original array? How can I make sure that set saves the values in the same order as it is present in the original array.

martineau
  • 119,623
  • 25
  • 170
  • 301
Zanam
  • 4,607
  • 13
  • 67
  • 143
  • 1
    Set: Unordered collection of unique items – Alan Kavanagh Jun 06 '16 at 02:09
  • On what basis does set generate the order then ? In my case it just picked a random date without any order. – Zanam Jun 06 '16 at 02:11
  • @Zanam you are right, the order of a set operation is random, there is no basis of order. If you need order or sorting you may need to use a different data structure or check out this answer, [Does Python have an ordered set?](http://stackoverflow.com/a/1653974/1248974) – chickity china chinese chicken Jun 06 '16 at 03:54
  • `np.unique` sorts its values, but can also return an index that lets you retrieve the original order, http://stackoverflow.com/questions/15637336/numpy-unique-with-order-preserved – hpaulj Jun 06 '16 at 04:22

0 Answers0