Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ein Datum wird bei bestimmten Sessionen nicht richtig geparst #17

Closed
bwueest opened this issue Jan 24, 2022 · 5 comments
Closed

Ein Datum wird bei bestimmten Sessionen nicht richtig geparst #17

bwueest opened this issue Jan 24, 2022 · 5 comments

Comments

@bwueest
Copy link

bwueest commented Jan 24, 2022

Danke vielmals für das tolle Swissparlpy-Modul! Ich habe versucht, Dein Beispiel für Votes auf den Download von Reden (Table ’Transcript’) anzuwenden, bekomme aber ein Fehler beim Parsen eines Datums.
Die Session 5002 (spp.get_data("Transcript", IdSession=5002)) geht interessanterweise, aber die Sessionen 5001 und 5003 nicht (z.B. spp.get_data("Transcript", IdSession=5001)), da bekomme ich jeweils den Fehler, dass strptime ein Datum nicht Parsen kann.
Hier mein Code:

import swissparlpy as spp
import pandas as pd
import os

__location__ = os.path.realpath(os.getcwd())
path2 = os.path.join(__location__, "transcripts50")

# download transcripts of one session and save as pickled DataFrame
def save_transcripts_of_session(id, path2):
    if not os.path.exists(path2):
        os.mkdir(path2)
    data = spp.get_data("Transcript", IdSession=id)
    print(f"{data.count} rows loaded.")
    df = pd.DataFrame(data)
    pickle_path = os.path.join(path2, f'{id}.pks')
    df.to_pickle(pickle_path)
    print(f"Saved pickle at {pickle_path}")

save_transcripts_of_session(5001, path2)

Hier die Fehlermeldung:
Fehler_Transcripts.txt
Mein Problem ist, dass nicht ersichtlich wird, welche Variable der Fehler betrifft und ob die Einträge mit diesem Datum schlicht ignoriert werden können?

@metaodi
Copy link
Owner

metaodi commented Jan 26, 2022

Hi @bwueest

since I linked this issue in SAP/python-pyodata#195 I'll reply in English. There seems to be a problem with the date parsing in pyodata, I library I'm using to make the calls to the swiss parliament webservice.

Here is my current workaround to get your script to run properly, add this code after your imports and before calling swissparlpy:

import pyodata
from pyodata.exceptions import PyODataModelError

# monkey-patch parse_datetime_literal
def patched_parse_datetime_literal(value):
    if value == '0000-00-00T00:00:00':
        return datetime.datetime(1970, 1, 1)
    try:
        return datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f')
    except ValueError:
        try:
            return datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S')
        except ValueError:
            try:
                return datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M')
            except ValueError:
                raise PyODataModelError(f'Cannot decode datetime from value {value}.')


pyodata.v2.model.parse_datetime_literal = patched_parse_datetime_literal

I hope there will soon be a proper fix for this.

@rettichschnidi
Copy link

rettichschnidi commented Jan 26, 2022

(Author of the code that causes the problem you reported...)

This should properly fix the issue: SAP/python-pyodata#197

@bwueest
Copy link
Author

bwueest commented Jan 26, 2022

Hej @metaodi and @rettichschnidi
Thank you so much for both the monkey patch and the fix. It runs smoothly now and I can close this issue.

@bwueest bwueest closed this as completed Jan 26, 2022
@metaodi
Copy link
Owner

metaodi commented Jan 31, 2022

@bwueest pyodata 1.9.0 has just been released, so if you upgrade pyodata (pip install --upgrade pyodata) to the latest version, no monkey-patch is needed.

@bwueest
Copy link
Author

bwueest commented Jan 31, 2022

thanks, this is great!

metaodi pushed a commit that referenced this issue Jan 31, 2022
This is due to issue #17 and an error in that library
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants