I cant remove a datetime object from my set?

Related searches
base = datetime.date.today()
dateset = set([base + datetime.timedelta(days=x) for x in range(20)]

for d in dateset:
 if d.weekday() == 0:

So as you can see I want to remove any datetime object that has a weekday() of 0.

However the above code throws the error RuntimeError: Set changed size during iteration

Any help is much appreciated.

Rather than creating the set then removing items, you could just only put items into the set that you want in the first place.

base = datetime.date.today()
dateset = set([base + datetime.timedelta(days=x) for x in range(20) if (base + datetime.timedelta(days=x)).weekday() != 0])
>>> dateset
{datetime.date(2019, 12, 4),
 datetime.date(2019, 12, 5),
 datetime.date(2019, 12, 6),
 datetime.date(2019, 12, 7),
 datetime.date(2019, 12, 8),
 datetime.date(2019, 12, 10),
 datetime.date(2019, 12, 11),
 datetime.date(2019, 12, 12),
 datetime.date(2019, 12, 13),
 datetime.date(2019, 12, 14),
 datetime.date(2019, 12, 15),
 datetime.date(2019, 12, 17),
 datetime.date(2019, 12, 18),
 datetime.date(2019, 12, 19),
 datetime.date(2019, 12, 20),
 datetime.date(2019, 12, 21),
 datetime.date(2019, 12, 22)}

8.1. datetime — Basic date and time types — Python v2.6.9 , Naive datetime objects are easy to understand and to work with, at the cost of ignoring some ValueError is raised if the date_string and format can't be parsed by If you merely want to remove the time zone object from an aware datetime dt� Because DateTime is an object, but an array of DateTime isn't an array of object. Arrays of value types are different than arrays of reference types so these two types of arrays are fundamentally incompatible. The value type array actually contains the values and the reference type array contains, well, only references.

You are iterating over an object which alters it's size during runtime, i.e. you iterate over dateset while discarding items from it in the loop with dateset.discard(d).

Get another iterator, say for example for idx in range(len(dateset)): to iterate over and remove the items from dateset accordingly.

Converting Strings to datetime in Python, If our input string to create a datetime object is in the same ISO 8601 format, we can easily parse it to a datetime object. Let's take a look at the code below: import � The following example uses the Date property to extract the date component of a DateTime value with its time component set to zero (or 0:00:00, or midnight). It also illustrates that, depending on the format string used when displaying the DateTime value, the time component can continue to appear in formatted output.

You can use a different set to collect the entries to be removed:

import datetime
base = datetime.date.today()
dateset = set([base + datetime.timedelta(days=x) for x in range(20)])
remove = set()

for d in dateset:
 if d.weekday() == 0:
dateset -= remove

Python Datetime Tutorial: Manipulate Times, Dates, and Time Spans, Creating Date Objects; Getting year and month from the date; Getting Of course , strptime() isn't magic — it can't turn any string into a date and� How to remove time portion of date in C# in DateTime object only? (20) I need to remove time portion of date time or probably have the date in following format in object form not in the form of string. 06/26/2009 00:00:00:000 I can not use any string conversion methods as I need the date in object form.

The problem here is that you're iterating over the thing you're changing (as the error tells us). A way you could achieve what you're going for is a set-comprehension. Like this:

base = datetime.date.today()
dateset = set([base + datetime.timedelta(days=x) for x in range(20)])

dateset = {d for d in dateset if d.weekday() != 0}

Note that we've inverted the condition to !=.

Data Cleaning Challenge: Parsing Dates, input/volcanic-eruptions/database.csv") # set seed for reproducibility Pandas uses the "object" dtype for storing various types of data types, but most often when rearranged so that they fit the default order datetime objects (year-month- day). because they haven't been parsed we can't interact with them in a useful way. Listing 4. The DateTime structure does not have similar Subtract methods. Only Subtract method is used to subtract the DateTime components. For example, if we need to subtract 12 days from a DateTime, we can create another DateTime object or a TimeSpan object with 12 days and subtract it from the DateTime.

You can't remove items from the set if you're looping over it. Either use a for loop and add the item to the set if the weekday is not 0

base = datetime.date.today()
dateset = set()
for x in range(20):
    date = base + datetime.timedelta(days=x)
    if date.weekday() != 0:

or use a list comprehension like this:

base = datetime.date.today()
dateset = set([base + datetime.timedelta(days=x) for x in range(20) if (base + datetime.timedelta(days=x)).weekday() != 0])

datetime in Python - Simplified Guide with Clear Examples, datetime is the standard module for working with dates in python. It provides 4 How to format the datetime object into any date format? It is normally used to add or remove a certain duration of time from datetime objects. Convert argument to datetime. Parameters arg int, float, str, datetime, list, tuple, 1-d array, Series, DataFrame/dict-like. The object to convert to a datetime. errors {‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’ If ‘raise’, then invalid parsing will raise an exception. If ‘coerce’, then invalid parsing will be set as

DateTime createdDate;} I have this value stored as tick in my db. If you browse it via sqlite browser, it is stored in bigint type instead of datetime (btw, no datetime field available in SQlite). This is the SQL statement to turn the datetime in unix to yyyy-mm-dd hh:mm:ss

The remove() method takes a single element as an argument and removes it from the set. Return Value from remove() The remove() removes the specified element from the set and updates the set.

Public Shared ReadOnly Property Today As DateTime Property Value DateTime. An object that is set to today's date, with the time component set to 00:00:00. Examples. The following example uses the Date property to retrieve the current date. It also illustrates how a DateTime value

  • Yes, you cannot remove items from a set while iterating over it. You should think of a different solution
  • A piece of feedback on your question: I see from the comments on the accepted answer that you wanted to create this list directly, rather than actually wanting to remove items from a set. As a result, most of the answers aren't actually giving you what you really wanted. Next time you should provide information about the core problem you want to solve - not just the small technical issue in your solution. This will help you get better answers.
  • @PirateNinjas Thanks for the feedback! Will definitely heed your advice.
  • Related to what @PirateNinjas said, I recommend these two resources for familiarizing yourself with the XY problem: xyproblem.info, meta.stackexchange.com/q/66377/628382.
  • @AlexanderCécile Thank you, this thread has been very helpful in more ways than one. I am going to check this out and adopt it going forward.
  • Thank you this is exactly what I was looking for. Would dateset = set([base + datetime.timedelta(days=x) for x in range(20) if (base + datetime.timedelta(days=x)).weekday() != 0 *and 6*]) also work?
  • In order to compare weekday() to two values, you need to do something like the following: dateset = set([base + datetime.timedelta(days=x) for x in range(20) if (base + datetime.timedelta(days=x)).weekday() not in [0, 6]])
  • This is because and 6 would evaluate the truth value of the integer 6 itself - which is always True - rather than checking if weekday() != 6.
  • Very helpful! Thank you very much.
  • This isn't a helpful answer. Clearly the poster is newer to these concepts and is unlikely to benefit from this advice.