from datetime import datetime
from datetime import timedelta
dateIs = datetime.today()
thisDay = datetime.strftime(dateIs, "%d")
thisDayInt = int(thisDay)
dateListTest = []
if(thisDayInt > 0):
while thisDayInt > 1:
thisDayInt -= 1
dateIs.replace(day=thisDayInt,hour=0,minute=0,second=0)
dateListTest.append(dateIs)
print(dateListTest)
Output
[datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371),
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371)]
What I want to do in this part is to get a list of the days from today to the beginning of the month one by one, but when I put them in the While loop, I cannot print the days by decrement. I don't know about a shorter path or where I'm skipping.
List comprehension
You could use a list comprehension for this.
from datetime import datetime
dateIs = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
thisDay = dateIs.day
dateListTest = [dateIs.replace(day=d) for d in range(thisDay-1, 0, -1)]
print(dateListTest)
[datetime.datetime(2021, 11, 17, 0, 0),
datetime.datetime(2021, 11, 16, 0, 0),
datetime.datetime(2021, 11, 15, 0, 0),
datetime.datetime(2021, 11, 14, 0, 0),
datetime.datetime(2021, 11, 13, 0, 0),
datetime.datetime(2021, 11, 12, 0, 0),
datetime.datetime(2021, 11, 11, 0, 0),
datetime.datetime(2021, 11, 10, 0, 0),
datetime.datetime(2021, 11, 9, 0, 0),
datetime.datetime(2021, 11, 8, 0, 0),
datetime.datetime(2021, 11, 7, 0, 0),
datetime.datetime(2021, 11, 6, 0, 0),
datetime.datetime(2021, 11, 5, 0, 0),
datetime.datetime(2021, 11, 4, 0, 0),
datetime.datetime(2021, 11, 3, 0, 0),
datetime.datetime(2021, 11, 2, 0, 0),
datetime.datetime(2021, 11, 1, 0, 0)]
Pandas
You could also do it with date_range from Pandas, though you will end up with a list of datetime.date rather than datetime.datetime.
from datetime import datetime
import pandas as pd
dateIs = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
thisDay = dateIs.day
dateListTestPandas =pd.date_range(start=dateIs.replace(day=thisDay-1), end=dateIs.replace(day=1), freq="-1D").date.tolist()
print(dateListTestPandas)
[datetime.date(2021, 11, 17),
datetime.date(2021, 11, 16),
datetime.date(2021, 11, 15),
datetime.date(2021, 11, 14),
datetime.date(2021, 11, 13),
datetime.date(2021, 11, 12),
datetime.date(2021, 11, 11),
datetime.date(2021, 11, 10),
datetime.date(2021, 11, 9),
datetime.date(2021, 11, 8),
datetime.date(2021, 11, 7),
datetime.date(2021, 11, 6),
datetime.date(2021, 11, 5),
datetime.date(2021, 11, 4),
datetime.date(2021, 11, 3),
datetime.date(2021, 11, 2),
datetime.date(2021, 11, 1)]
This question already has answers here:
Python List Group by Date
(2 answers)
Closed 3 years ago.
I have this list :
list = [datetime.datetime(2019, 10, 20, 16, 37), datetime.datetime(2019, 10, 20, 16, 50, 7), datetime.datetime(2019, 11, 21, 16, 51, 47), datetime.datetime(2019, 11, 21, 10, 51, 20), datetime.datetime(2019, 10, 21, 10, 53, 22), datetime.datetime(2019, 11, 21, 10, 58, 27)]
I want to group elements with same date in list , how to do this please ?
[datetime.datetime(2019, 10, 20, 16, 37) , datetime.datetime(2019, 10, 20, 16, 50, 7) ]
[datetime.datetime(2019, 10, 21, 10, 53, 22)]
[datetime.datetime(2019, 11, 21, 16, 51, 47), datetime.datetime(2019, 11, 21, 10, 51, 20),datetime.datetime(2019, 11, 21, 10, 58, 27)]
Use itertools.groupby:
from itertools import groupby
f = lambda x:x.date()
[[i for i in g] for k, g in groupby(sorted(l, key=f), key=f)]
Output:
[[datetime.datetime(2019, 10, 20, 16, 37),
datetime.datetime(2019, 10, 20, 16, 50, 7)],
[datetime.datetime(2019, 10, 21, 10, 53, 22)],
[datetime.datetime(2019, 11, 21, 16, 51, 47),
datetime.datetime(2019, 11, 21, 10, 51, 20),
datetime.datetime(2019, 11, 21, 10, 58, 27)]]
You could just use itertools.groupby like,
>>> import itertools
>>> import datetime
>>> x
[datetime.datetime(2019, 10, 20, 16, 37), datetime.datetime(2019, 10, 20, 16, 50, 7), datetime.datetime(2019, 11, 21, 16, 51, 47), datetime.datetime(2019, 11, 21, 10, 51, 20), datetime.datetime(2019, 10, 21, 10, 53, 22), datetime.datetime(2019, 11, 21, 10, 58, 27)]
>>> s = [list(v) for k,v in itertools.groupby(sorted(x), lambda x: (x.year, x.month, x.day))]
>>>
>>>
>>> pprint.pprint(s)
[[datetime.datetime(2019, 10, 20, 16, 37),
datetime.datetime(2019, 10, 20, 16, 50, 7)],
[datetime.datetime(2019, 10, 21, 10, 53, 22)],
[datetime.datetime(2019, 11, 21, 10, 51, 20),
datetime.datetime(2019, 11, 21, 10, 58, 27),
datetime.datetime(2019, 11, 21, 16, 51, 47)]]
>>>
Using dict.setdefault and a simple iteration.
Ex:
import datetime
data = [datetime.datetime(2019, 10, 20, 16, 37), datetime.datetime(2019, 10, 20, 16, 50, 7), datetime.datetime(2019, 11, 21, 16, 51, 47), datetime.datetime(2019, 11, 21, 10, 51, 20), datetime.datetime(2019, 10, 21, 10, 53, 22), datetime.datetime(2019, 11, 21, 10, 58, 27)]
result = {}
for i in data:
result.setdefault(i.strftime("%Y%m%d"), []).append(i)
print(list(result.values()))
Output:
[[datetime.datetime(2019, 10, 20, 16, 37),
datetime.datetime(2019, 10, 20, 16, 50, 7)],
[datetime.datetime(2019, 11, 21, 16, 51, 47),
datetime.datetime(2019, 11, 21, 10, 51, 20),
datetime.datetime(2019, 11, 21, 10, 58, 27)],
[datetime.datetime(2019, 10, 21, 10, 53, 22)]]
I am trying to create a stacked histogram with datetime objects, but I get the following error:
TypeError: unorderable types: datetime.datetime() < float()
The code does work when I either convert the objects to timestamps or when I use one range of data (no stacking).
import datetime
import matplotlib.pyplot as plt
data = [[datetime.datetime(2015, 12, 24, 21, 13, 45), datetime.datetime(2015, 12, 30, 23, 37, 8), datetime.datetime(2015, 12, 30, 19, 43, 18), datetime.datetime(2015, 12, 30, 16, 14, 12), datetime.datetime(2015, 12, 30, 11, 32, 8), datetime.datetime(2015, 12, 29, 6, 29, 25), datetime.datetime(2015, 12, 28, 22, 54, 49), datetime.datetime(2015, 12, 28, 18, 41, 50), datetime.datetime(2015, 12, 28, 14, 25, 42), datetime.datetime(2015, 12, 28, 3, 1, 34), datetime.datetime(2015, 12, 27, 21, 10, 20), datetime.datetime(2015, 12, 27, 11, 29, 38), datetime.datetime(2015, 12, 26, 20, 34, 14), datetime.datetime(2015, 12, 26, 16, 58, 47), datetime.datetime(2015, 12, 26, 10, 54, 40), datetime.datetime(2015, 12, 25, 18, 17, 42), datetime.datetime(2015, 12, 24, 15, 44, 58), datetime.datetime(2015, 12, 25, 17, 25, 9), datetime.datetime(2015, 12, 25, 12, 33, 7), datetime.datetime(2015, 12, 30, 19, 26, 15), datetime.datetime(2015, 12, 30, 12, 46, 13), datetime.datetime(2015, 12, 30, 3, 38, 24), datetime.datetime(2015, 12, 25, 21, 11, 59), datetime.datetime(2015, 12, 25, 13, 30, 34), datetime.datetime(2015, 12, 24, 14, 6, 20)], [datetime.datetime(2015, 12, 28, 20, 59, 53), datetime.datetime(2015, 12, 27, 14, 3, 41), datetime.datetime(2015, 12, 26, 9, 37, 17)], [datetime.datetime(2015, 12, 29, 17, 18, 32)], [datetime.datetime(2015, 12, 29, 23, 15, 24)]]
fig, histograms = plt.subplots(5, 1, sharex=True, squeeze=False)
h = histograms[1][0]
h.hist(data, stacked=True)
This is the code without stacking:
import datetime
import matplotlib.pyplot as plt
data = [datetime.datetime(2015, 12, 24, 21, 13, 45), datetime.datetime(2015, 12, 30, 23, 37, 8), datetime.datetime(2015, 12, 30, 19, 43, 18), datetime.datetime(2015, 12, 30, 16, 14, 12), datetime.datetime(2015, 12, 30, 11, 32, 8), datetime.datetime(2015, 12, 29, 6, 29, 25), datetime.datetime(2015, 12, 28, 22, 54, 49), datetime.datetime(2015, 12, 28, 18, 41, 50), datetime.datetime(2015, 12, 28, 14, 25, 42), datetime.datetime(2015, 12, 28, 3, 1, 34), datetime.datetime(2015, 12, 27, 21, 10, 20), datetime.datetime(2015, 12, 27, 11, 29, 38), datetime.datetime(2015, 12, 26, 20, 34, 14), datetime.datetime(2015, 12, 26, 16, 58, 47), datetime.datetime(2015, 12, 26, 10, 54, 40), datetime.datetime(2015, 12, 25, 18, 17, 42), datetime.datetime(2015, 12, 24, 15, 44, 58), datetime.datetime(2015, 12, 25, 17, 25, 9), datetime.datetime(2015, 12, 25, 12, 33, 7), datetime.datetime(2015, 12, 30, 19, 26, 15), datetime.datetime(2015, 12, 30, 12, 46, 13), datetime.datetime(2015, 12, 30, 3, 38, 24), datetime.datetime(2015, 12, 25, 21, 11, 59), datetime.datetime(2015, 12, 25, 13, 30, 34), datetime.datetime(2015, 12, 24, 14, 6, 20), datetime.datetime(2015, 12, 28, 20, 59, 53), datetime.datetime(2015, 12, 27, 14, 3, 41), datetime.datetime(2015, 12, 26, 9, 37, 17), datetime.datetime(2015, 12, 29, 17, 18, 32), datetime.datetime(2015, 12, 29, 23, 15, 24)]
fig, histograms = plt.subplots(5, 1, sharex=True, squeeze=False)
h = histograms[1][0]
h.hist(data, stacked=True)
NOTE:
As per answers, this is considered a bug. For future visitors I have filed a bug report https://github.com/matplotlib/matplotlib/issues/5898 in case you want to track progress
This is a bug, revealed by version 1.5.x supporting histograms of single series of datetime type data. Previous versions of matplotlib would not histogram datetime data whether stacked or not, failing with a similar error that said datetime could not be compared with float.
The Exception is thrown by this line of code. As you can see, that is called only when bin edges are not specified and is trying to find the minimum in the time series (comparing it with np.inf and taking the minimum of those). You can workaround this by specifying bin edges in the call, but then that leads to a further failure as the numpy histogram function called under the hood looks for less than zero width bins.
"Under the hood" when a single list of datetime.datetime objects is passed to the pyplot.hist() function, the data are actually converted to UNIX epoch timestamps (you can guess this from the labels of the x axis). This is not done when the input is a list of lists of datetime.datetime objects.
At that stage, I think we have to call it a bug and you will have to use timestamp as you have already discovered - e.g. h.hist([[t.timestamp() for t in s] for s in data], stacked=True). You can still give the bin labels in date format, even though the actual data being histogrammed are timestamps, thus this should be transparent to the user.
I'll have a look to see whether I can find a nicer workaround / fix and possibly raise an issue on the matplotlib github.
Code that works (matplotlib 1.5.1, Python 3), albeit a bit messy
import datetime
import matplotlib.pyplot as plt
data = [[datetime.datetime(2015, 12, 24, 21, 13, 45), datetime.datetime(2015, 12, 30, 23, 37, 8), datetime.datetime(2015, 12, 30, 19, 43, 18), datetime.datetime(2015, 12, 30, 16, 14, 12), datetime.datetime(2015, 12, 30, 11, 32, 8), datetime.datetime(2015, 12, 29, 6, 29, 25), datetime.datetime(2015, 12, 28, 22, 54, 49), datetime.datetime(2015, 12, 28, 18, 41, 50), datetime.datetime(2015, 12, 28, 14, 25, 42), datetime.datetime(2015, 12, 28, 3, 1, 34), datetime.datetime(2015, 12, 27, 21, 10, 20), datetime.datetime(2015, 12, 27, 11, 29, 38), datetime.datetime(2015, 12, 26, 20, 34, 14), datetime.datetime(2015, 12, 26, 16, 58, 47), datetime.datetime(2015, 12, 26, 10, 54, 40), datetime.datetime(2015, 12, 25, 18, 17, 42), datetime.datetime(2015, 12, 24, 15, 44, 58), datetime.datetime(2015, 12, 25, 17, 25, 9), datetime.datetime(2015, 12, 25, 12, 33, 7), datetime.datetime(2015, 12, 30, 19, 26, 15), datetime.datetime(2015, 12, 30, 12, 46, 13), datetime.datetime(2015, 12, 30, 3, 38, 24), datetime.datetime(2015, 12, 25, 21, 11, 59), datetime.datetime(2015, 12, 25, 13, 30, 34), datetime.datetime(2015, 12, 24, 14, 6, 20)], [datetime.datetime(2015, 12, 28, 20, 59, 53), datetime.datetime(2015, 12, 27, 14, 3, 41), datetime.datetime(2015, 12, 26, 9, 37, 17)], [datetime.datetime(2015, 12, 29, 17, 18, 32)], [datetime.datetime(2015, 12, 29, 23, 15, 24)]]
fig, histograms = plt.subplots(5, 1, sharex=True, squeeze=False)
h = histograms[1][0]
h.hist([[t.timestamp() for t in l] for l in data], stacked=True)
locs, labels = plt.xticks()
plt.xticks(locs,[datetime.datetime.fromtimestamp(t) for t in locs], rotation='vertical')
plt.gcf().subplots_adjust(bottom=0.4)
fig.set_size_inches(4, 15)
plt.show()
Produces
I have an array in python called dates with this content:
[datetime.datetime(2012, 1, 11, 17, 24, 12, 676000), datetime.datetime(2012, 2, 3, 11, 25, 17, 73000), datetime.datetime(2012, 2, 3, 14, 9, 23, 699000), datetime.datetime(2012, 2, 4, 9, 15, 26, 644000), datetime.datetime(2012, 2, 4, 17, 14, 36, 65000), datetime.datetime(2012, 2, 5, 6, 18, 31, 139000), datetime.datetime(2012, 2, 5, 14, 55, 28, 62000), datetime.datetime(2012, 2, 5, 18, 28, 59, 379000), datetime.datetime(2012, 2, 6, 12, 24, 21, 768000), datetime.datetime(2012, 2, 6, 17, 32, 46, 675000), datetime.datetime(2012, 2, 14, 11, 33, 6, 74000), datetime.datetime(2012, 2, 14, 11, 36, 48, 11000), datetime.datetime(2012, 2, 16, 8, 54, 14, 175000), datetime.datetime(2012, 2, 16, 18, 33, 9, 200000), datetime.datetime(2012, 2, 20, 8, 41, 2, 550000), datetime.datetime(2012, 2, 20, 9, 4, 37, 446000), datetime.datetime(2012, 2, 20, 10, 10, 42, 950000), datetime.datetime(2012, 2, 20, 21, 21, 21, 986000), datetime.datetime(2012, 2, 21, 9, 1, 8, 429000), datetime.datetime(2012, 2, 21, 12, 5, 20, 475000), datetime.datetime(2012, 2, 21, 13, 23, 25, 281000), datetime.datetime(2012, 2, 21, 15, 4, 29, 366000), datetime.datetime(2012, 2, 21, 15, 12, 21, 729000), datetime.datetime(2012, 2, 21, 15, 29, 10, 723000), datetime.datetime(2012, 2, 21, 18, 10, 24, 822000), datetime.datetime(2012, 2, 22, 10, 42, 11, 689000), datetime.datetime(2012, 2, 22, 13, 10, 1, 309000), datetime.datetime(2012, 2, 22, 20, 28, 34, 260000), datetime.datetime(2012, 2, 27, 17, 53, 19, 225000), datetime.datetime(2012, 2, 28, 8, 13, 57, 139000), datetime.datetime(2012, 3, 2, 7, 55, 11, 505000), datetime.datetime(2012, 3, 2, 21, 6, 35, 270000), datetime.datetime(2012, 3, 5, 8, 10, 47, 76000), datetime.datetime(2012, 3, 5, 9, 15, 15, 448000), datetime.datetime(2012, 3, 7, 18, 15, 35, 401000), datetime.datetime(2012, 3, 15, 8, 6, 56, 968000), datetime.datetime(2012, 3, 16, 15, 34, 10, 59000), datetime.datetime(2012, 3, 20, 18, 19, 13, 687000), datetime.datetime(2012, 3, 22, 8, 50, 28, 983000), datetime.datetime(2012, 3, 23, 8, 26, 5, 468000), datetime.datetime(2012, 3, 27, 7, 50, 14, 474000), datetime.datetime(2012, 3, 27, 15, 14, 35, 59000), datetime.datetime(2012, 4, 5, 7, 23, 1, 374000), datetime.datetime(2012, 4, 6, 13, 8, 59, 578000), datetime.datetime(2012, 4, 6, 13, 34, 24, 843000), datetime.datetime(2012, 4, 6, 15, 35, 40, 538000), datetime.datetime(2012, 4, 10, 7, 0, 37, 455000), datetime.datetime(2012, 4, 10, 7, 12, 37, 199000), datetime.datetime(2012, 4, 10, 7, 39, 16, 366000), datetime.datetime(2012, 4, 10, 7, 55, 51, 228000), datetime.datetime(2012, 4, 11, 7, 53, 31, 699000), datetime.datetime(2012, 4, 11, 15, 32, 21, 582000), datetime.datetime(2012, 4, 13, 10, 22, 4, 673000), datetime.datetime(2012, 4, 16, 7, 17, 20, 578000), datetime.datetime(2012, 11, 29, 16, 5, 21, 53000), datetime.datetime(2012, 11, 29, 16, 6, 15, 244000), datetime.datetime(2013, 1, 25, 9, 45, 48, 921000), datetime.datetime(2013, 2, 4, 18, 1, 1, 418000), datetime.datetime(2013, 2, 5, 6, 14, 55, 728000), datetime.datetime(2013, 2, 5, 17, 2, 11, 959000), datetime.datetime(2013, 2, 7, 6, 4, 8, 629000), datetime.datetime(2013, 2, 7, 18, 6, 47, 247000), datetime.datetime(2013, 2, 8, 5, 36, 55, 702000), datetime.datetime(2013, 2, 8, 8, 51, 46, 261000), datetime.datetime(2013, 2, 12, 5, 56, 37, 233000), datetime.datetime(2013, 2, 12, 16, 6, 25, 126000), datetime.datetime(2013, 2, 13, 7, 45, 33, 448000), datetime.datetime(2013, 2, 13, 10, 43, 15, 749000), datetime.datetime(2013, 2, 14, 6, 10, 27, 562000), datetime.datetime(2013, 2, 14, 16, 44, 45, 469000), datetime.datetime(2013, 2, 15, 6, 3, 12, 787000), datetime.datetime(2013, 2, 15, 14, 8, 40, 281000), datetime.datetime(2013, 2, 17, 11, 46, 41, 983000), datetime.datetime(2013, 2, 20, 15, 32, 52, 455000), datetime.datetime(2013, 2, 21, 16, 0, 40, 165000), datetime.datetime(2013, 2, 22, 9, 12, 55, 32000), datetime.datetime(2013, 2, 22, 15, 11, 45, 979000), datetime.datetime(2013, 2, 25, 6, 52, 49, 991000), datetime.datetime(2013, 2, 25, 8, 52, 8, 947000), datetime.datetime(2013, 2, 25, 9, 27, 7, 716000), datetime.datetime(2013, 2, 25, 9, 33, 21, 121000), datetime.datetime(2013, 2, 26, 7, 15, 0, 135000), datetime.datetime(2013, 2, 26, 16, 15, 39, 693000), datetime.datetime(2013, 2, 27, 6, 33, 23, 745000), datetime.datetime(2013, 2, 27, 17, 28, 47, 793000), datetime.datetime(2013, 2, 28, 5, 43, 32, 479000), datetime.datetime(2013, 2, 28, 17, 22, 15, 510000), datetime.datetime(2013, 3, 1, 6, 54, 21, 676000), datetime.datetime(2013, 3, 1, 15, 47, 19, 912000), datetime.datetime(2013, 3, 4, 17, 39, 55, 809000), datetime.datetime(2013, 3, 5, 6, 40, 35, 101000), datetime.datetime(2013, 3, 5, 17, 5, 4, 324000), datetime.datetime(2013, 3, 6, 6, 39, 42, 235000), datetime.datetime(2013, 3, 6, 16, 6, 29, 410000), datetime.datetime(2013, 3, 7, 6, 32, 56, 197000), datetime.datetime(2013, 3, 7, 17, 31, 39, 249000), datetime.datetime(2013, 3, 8, 6, 56, 44, 369000), datetime.datetime(2013, 3, 11, 7, 17, 20, 748000), datetime.datetime(2013, 3, 11, 17, 27, 43, 102000), datetime.datetime(2013, 3, 12, 7, 10, 24, 751000), datetime.datetime(2013, 3, 12, 10, 23, 44, 759000), datetime.datetime(2013, 3, 12, 15, 42, 20, 461000), datetime.datetime(2013, 3, 13, 7, 12, 40, 494000), datetime.datetime(2013, 3, 13, 12, 7, 24, 986000), datetime.datetime(2013, 3, 14, 6, 52, 10, 779000), datetime.datetime(2013, 3, 14, 16, 39, 12, 776000), datetime.datetime(2013, 3, 15, 7, 4, 26, 454000), datetime.datetime(2013, 3, 15, 16, 40, 37, 98000), datetime.datetime(2013, 3, 18, 6, 53, 56, 937000), datetime.datetime(2013, 3, 18, 16, 53, 26, 914000), datetime.datetime(2013, 3, 19, 6, 34, 41, 813000), datetime.datetime(2013, 3, 19, 17, 19, 59, 721000), datetime.datetime(2013, 3, 20, 6, 57, 37, 141000), datetime.datetime(2013, 3, 20, 15, 15, 43, 458000), datetime.datetime(2013, 3, 21, 15, 36, 12, 949000), datetime.datetime(2013, 3, 22, 6, 57, 21, 973000), datetime.datetime(2013, 3, 22, 15, 36, 14, 388000), datetime.datetime(2013, 3, 25, 7, 0, 43, 602000), datetime.datetime(2013, 3, 25, 18, 27, 0, 693000), datetime.datetime(2013, 3, 26, 17, 20, 48, 194000), datetime.datetime(2013, 3, 27, 7, 11, 17, 665000), datetime.datetime(2013, 3, 27, 18, 27, 41, 894000), datetime.datetime(2013, 3, 28, 7, 2, 8, 624000), datetime.datetime(2013, 3, 28, 11, 12, 22, 53000), datetime.datetime(2013, 4, 3, 5, 45, 23, 995000), datetime.datetime(2013, 4, 4, 6, 5, 39, 243000), datetime.datetime(2013, 4, 8, 6, 4, 34, 667000), datetime.datetime(2013, 4, 8, 17, 6, 8, 718000), datetime.datetime(2013, 4, 9, 6, 2, 32, 813000), datetime.datetime(2013, 4, 9, 15, 16, 46, 622000), datetime.datetime(2013, 4, 10, 5, 26, 16, 694000), datetime.datetime(2013, 4, 10, 18, 50, 54, 809000), datetime.datetime(2013, 4, 11, 15, 12, 29, 376000), datetime.datetime(2013, 4, 12, 6, 9, 38, 925000), datetime.datetime(2013, 4, 12, 14, 42, 32, 607000), datetime.datetime(2013, 4, 15, 10, 0, 59, 995000), datetime.datetime(2013, 4, 15, 10, 11, 42, 16000), datetime.datetime(2013, 4, 16, 6, 8, 3, 838000), datetime.datetime(2013, 4, 16, 15, 27, 35, 147000), datetime.datetime(2013, 4, 17, 6, 4, 44, 272000), datetime.datetime(2013, 4, 17, 15, 23, 0, 924000), datetime.datetime(2013, 4, 18, 6, 9, 55, 454000), datetime.datetime(2013, 4, 18, 15, 5, 43, 601000), datetime.datetime(2013, 4, 19, 6, 0, 38, 132000), datetime.datetime(2013, 4, 19, 16, 35, 26, 14000), datetime.datetime(2013, 4, 19, 17, 44, 17, 116000), datetime.datetime(2013, 4, 19, 17, 51, 48, 43000), datetime.datetime(2013, 4, 19, 17, 54, 30, 44000), datetime.datetime(2013, 4, 21, 14, 58, 56, 363000), datetime.datetime(2013, 4, 21, 15, 8, 11, 276000), datetime.datetime(2013, 4, 23, 6, 24, 57, 124000), datetime.datetime(2013, 4, 23, 15, 44, 30, 503000), datetime.datetime(2013, 4, 25, 6, 13, 9, 269000), datetime.datetime(2013, 4, 25, 15, 41, 11, 370000), datetime.datetime(2013, 4, 26, 6, 2, 17, 877000), datetime.datetime(2013, 4, 27, 16, 17, 34, 97000), datetime.datetime(2013, 4, 27, 18, 20, 57, 975000), datetime.datetime(2013, 4, 29, 10, 17, 41, 746000), datetime.datetime(2013, 4, 29, 16, 45, 18, 65000), datetime.datetime(2013, 4, 30, 6, 13, 2, 333000), datetime.datetime(2013, 4, 30, 15, 3, 22, 343000), datetime.datetime(2013, 5, 1, 7, 22, 40, 401000), datetime.datetime(2013, 5, 1, 11, 16, 38, 525000), datetime.datetime(2013, 5, 2, 6, 7, 7, 749000), datetime.datetime(2013, 5, 3, 12, 48, 22, 617000), datetime.datetime(2013, 5, 6, 6, 1, 1, 168000), datetime.datetime(2013, 5, 6, 14, 56, 48, 236000), datetime.datetime(2013, 5, 7, 16, 47, 4, 597000), datetime.datetime(2013, 5, 8, 15, 26, 52, 105000), datetime.datetime(2013, 5, 10, 6, 10, 39, 379000), datetime.datetime(2013, 5, 13, 6, 9, 57, 990000), datetime.datetime(2013, 5, 13, 19, 56, 15, 354000), datetime.datetime(2013, 5, 15, 16, 39, 9, 127000), datetime.datetime(2013, 5, 16, 5, 59, 27, 609000), datetime.datetime(2013, 5, 16, 14, 18, 33, 253000), datetime.datetime(2013, 5, 17, 6, 20, 11, 853000), datetime.datetime(2013, 5, 21, 15, 38, 10, 53000), datetime.datetime(2013, 5, 22, 5, 59, 8, 126000), datetime.datetime(2013, 5, 22, 15, 48, 55, 877000), datetime.datetime(2013, 5, 23, 5, 47, 4, 779000), datetime.datetime(2013, 5, 23, 16, 59, 16, 948000), datetime.datetime(2013, 5, 24, 10, 57, 34, 831000), datetime.datetime(2013, 5, 24, 12, 29, 17, 332000), datetime.datetime(2013, 5, 27, 17, 0, 14, 513000), datetime.datetime(2013, 6, 20, 7, 28, 45, 975000), datetime.datetime(2013, 6, 20, 13, 31, 13, 228000), datetime.datetime(2013, 6, 21, 6, 18, 47, 789000), datetime.datetime(2013, 7, 1, 6, 12, 3, 640000), datetime.datetime(2013, 7, 1, 14, 33, 9, 251000), datetime.datetime(2013, 7, 2, 14, 59, 0, 421000), datetime.datetime(2013, 7, 3, 6, 12, 58, 282000), datetime.datetime(2013, 7, 3, 17, 23, 38, 745000), datetime.datetime(2013, 7, 5, 13, 40, 44, 719000), datetime.datetime(2013, 7, 9, 14, 51, 27, 348000), datetime.datetime(2013, 7, 10, 5, 12, 3, 104000)]
It should be ordered by date. What I need to know is how many days apear on this array. If there are many dates of the same day, I'll count only 1.
I could do it "by hand", iterating over each point and checking to a temp variable and count the days, but isn't there a faster, proper way to "unique" by days?
thanks
You can use the datetime.date() method:
s = {d.date() for d in dates}
print len(s)
Since dates are hashable, you can put them in a set just fine...
Note that you could also get a count of how many times each date appeared:
import collections
print collections.Counter(d.date() for d in dates)
Or, even do a list of datetime instances keyed by date:
import collections
d = collections.defaultdict(list)
for dt in dates:
d[dt.date()].append(dt)
Although, I suppose that since the input is sorted, you could do the same thing more or less with itertools.groupby:
for date, dt_group in itertools.groupby(dates, key=lambda dt: dt.date()):
print date, list(dt_group)
If you simply want to count how many unique days there are, the following works:
print len({(i.day,i.month,i.year) for i in dates})
This is to ensure that it is infact the same date and not just the same day number, since 1st of November has the say .day as the 1st of december but they are obviously not the same day.