0

I have a variable with an array made up of datetimes I've pulled from a CSV file, that looks something like this.

[datetime.date(2020, 9, 2), datetime.date(2020, 9, 2), datetime.date(2020, 10, 2), datetime.date(2020, 10, 2), datetime.date(2020, 10, 2), datetime.date(2020, 10, 2), datetime.date(2020, 11, 2), ..., datetime.date(2020, 10, 28)]

And I have variable with an array also pulled from the CSV file with temperatures as floats, of similar length.

[24.9, 23.4, 23.8, 22.9, 23.6, ..., 25.2]

I'm looking for a way to sort both of these arrays so the temperature value corresponding to the date value, have the same position in new arrays. The new arrays should be sorted so that the date arrays only includes the same date, so it can be used for making multiple boxplots with the temperature from each day. For example:

[datetime.date(2020, 9, 2), datetime.date(2020, 9, 2)]
[datetime.date(2020, 10, 2), datetime.date(2020, 10, 2), datetime.date(2020, 10, 2)]
...

and
[24.9, 23.4]
[23.8, 22.9, 23.6]
...

I guess there's some way to do it with for loops, but I have no idea how.

P.S. it's not going to be the same dataset each time the program is run, and each dataset contains 8000 entries.

2 Answers 2

2

If I understood you correctly how about this:

import datetime

dates = [datetime.date(2020, 9, 2), datetime.date(2020, 9, 2), datetime.date(2020, 10, 2), datetime.date(2020, 10, 2), datetime.date(2020, 10, 2), datetime.date(2020, 10, 2), datetime.date(2020, 11, 2), datetime.date(2020, 10, 28)]
temps = [24.9, 23.4, 23.8, 22.9, 23.6, 25.2, 10, 20]

out = {}
for idx, d in enumerate(dates):
    if not d in out:
        out[d] = []
    out[d].append(temps[idx])

Then out will contain all the dates with corresponding temperatures:

{datetime.date(2020, 9, 2): [24.9, 23.4],
 datetime.date(2020, 10, 2): [23.8, 22.9, 23.6, 25.2],
 datetime.date(2020, 11, 2): [10],
 datetime.date(2020, 10, 28): [20]}
Sign up to request clarification or add additional context in comments.

1 Comment

It looks like it worked, even though I just noticed I wrote the dates in the example wrong, it still seems like it worked perfectly! Thanks a lot for the quick answer!
1

Expanding on @alex's answer with the handy yet underused dict.setdefault function:

>>> out = {}
>>> for i, d in enumerate(dates):
...    out.setdefault(d, []).append(temps[i])

{ datetime.date(2020, 9, 2): [24.9, 23.4],
  datetime.date(2020, 10, 2): [23.8, 22.9, 23.6, 25.2],
  datetime.date(2020, 10, 28): [20],
  datetime.date(2020, 11, 2): [10] }

2 Comments

I guess it works too, but I am not familiar with what dict.setdefault specifically does since I am still new to programming. But very much appreciated
No worries, it dict.setdefault takes a key as 1st param and any value as 2nd param. It will assign the value to that key if that key doesn't exist in the dict. The nice thing is the function returns a reference to that value (here a list) which means you can chain an append call on that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.