0

My problem is to move the year one day ahead so my calculations start with the first day of the year to be a Monday preferably. It can ignore the previous day e.g if 1st Jan is a Sunday, it can ignore it and push the Monday as the 1st day. The logic I have implemented is to only get the data of Mondays only e.g 1st day of the week.


X=[]
Y=[]
for item in data['Elements']:
    for sub_item in item['TimeSpans']:
        if (item['Date'].startswith("2017")):
            iso_day = datetime.datetime.strptime(item['Date'], '%Y-%m-%dT%H:%M:%S').isocalendar()       #Moving Date Logic
            if (iso_day[2] == 1):
                X.append(iso_day)
                Y.append(sub_item['Value'])

How do I make it so show everyday instead of this Monday only whilst keeping my above yearly problem in mind? the condition in the if statement makes it start with Mondays only. How can I manipulate the statement so that it starts with Mondays initially but also then push out the data of other days as well when once started with a Monday.

my json is like this :

{
    "SpotKey": "79",
    "SpotName": "ELIX",
    "Denomination": "eur/mwh",
    "Elements": [
      {
        "Date": "2017-01-01T00:00:00",
        "Base": 36.8696,
        "Peak": 36.0125,
        "TimeSpans": [
          {
            "TimeSpan": "00:00-01:00",
            "Value": 46.43
          },
          {
            "TimeSpan": "01:00-02:00",
            "Value": 42.43
          }
        ]
      },
      {
        "Date": "2017-01-02T00:00:00",
        "Base": 53.7413,
        "Peak": 63.0317,
        "TimeSpans": [
          {
            "TimeSpan": "00:00-01:00",
            "Value": 41.18
          },
          {
            "TimeSpan": "01:00-02:00",
            "Value": 37.34
          }
        ]
      }
    ]
}
  • Check dateutil - it can do calculations like "next monday from x", "3rd monday of month" etc. – h4z3 Mar 10 '20 at 09:30
  • you mean like [this](https://stackoverflow.com/questions/6558535/find-the-date-for-the-first-monday-after-a-given-a-date)? – FObersteiner Mar 10 '20 at 10:16

1 Answers1

0

I think you can find the first Monday of the year and the last day of the year. Then just compare each day from the json to these days.

Something like:

import datetime 
qyear = 2017
first_day = datetime.datetime(qyear, 1, 1)
if (first_day.weekday() == 6):
    first_day = datetime.datetime(qyear, 1, 2)
last_day = datetime.datetime(qyear, 12, 31)
if (iso_day >= first_day and  iso_day <= last_day):
Ilya S
  • 141
  • 5
  • I'm not sure I understand what you are trying to say. Can you please elaborate on it a little more? –  Mar 10 '20 at 09:42
  • I think I misunderstood your question. What exactly do you want want to get? Do you want to get the data from your json for all the first Mondays of each year? Ignoring all other data? – Ilya S Mar 10 '20 at 09:54
  • I want the data which is STARTING from Mondays AND includes all other days as well till the end of year (Including all the other days as well) –  Mar 10 '20 at 10:16