[Python] Reading CSV dates with pandas returns datetime instead of Timestamp

Discussão em 'Python' iniciado por Stack, Setembro 2, 2025.

  1. Stack

    Stack Membro Participativo

    Problem description:

    I'm trying to read historical stock prices from CSV format into pandas Dataframe, but one funny thing I have noticed so far - when reading certain row numbers, the dates column type is changed from pandas.Timestamp into datetime - how does that work? How can I read pandas.Timestamp then?

    Minimum reproduction example:

    I have inspected my CSV file, and here is a minimal needed data example from it.

    import pandas as pd
    file = open('temp.csv', 'w')
    file.write(
    """Local time,Open,High,Low,Close,Volume
    28.02.2014 02:00:00.000 GMT+0200,1.37067,1.38250,1.36943,1.38042,176839.0313
    01.04.2014 03:00:00.000 GMT+0300,1.37742,1.38156,1.37694,1.37937,95386.0703""")
    file.close()

    data = pd.read_csv('temp.csv', parse_dates = ["Local time"])
    print(type(data['Local time'][0]))


    Result: <class 'datetime.datetime'>

    VERSUS

    import pandas as pd
    file = open('temp.csv', 'w')
    file.write(
    """Local time,Open,High,Low,Close,Volume
    28.02.2014 02:00:00.000 GMT+0200,1.37067,1.38250,1.36943,1.38042,176839.0313""")
    file.close()

    data = pd.read_csv('temp.csv', parse_dates = ["Local time"])
    print(type(data['Local time'][0]))

    file = open('temp.csv', 'w')
    file.write(
    """Local time,Open,High,Low,Close,Volume
    01.04.2014 03:00:00.000 GMT+0300,1.37742,1.38156,1.37694,1.37937,95386.0703""")
    file.close()

    data = pd.read_csv('temp.csv', parse_dates = ["Local time"])
    print(type(data['Local time'][0]))

    file = open('temp.csv', 'w')
    file.write(
    """Local time,Open,High,Low,Close,Volume
    02.03.2014 02:00:00.000 GMT+0200,1.37620,1.37882,1.37586,1.37745,5616.04
    03.03.2014 02:00:00.000 GMT+0200,1.37745,1.37928,1.37264,1.37357,136554.6563
    04.03.2014 02:00:00.000 GMT+0200,1.37356,1.37820,1.37211,1.37421,124863.8203""")
    file.close()

    data = pd.read_csv('temp.csv', parse_dates = ["Local time"])
    print(type(data['Local time'][0]))


    Result: <class 'pandas._libs.tslibs.timestamps.Timestamp'>

    Result: <class 'pandas._libs.tslibs.timestamps.Timestamp'>

    Result: <class 'pandas._libs.tslibs.timestamps.Timestamp'>

    Versions:

    pandas==1.2.3 pandas-datareader==0.9.0

    Summary:

    I need to read pandas.Timestamp because of some latter data manipulations, not datetime, and have no ideas what's wrong here - hope you guys can help...

    I have created a GitHub issue as well, but it's not triaged yet.

    Continue reading...

Compartilhe esta Página