Python Forum
problems with reading csv file.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problems with reading csv file.
#1
Sad 
i have watched a tutorial about a neural network which i would like to train with this csv file. however, i have the problem that i always get this error when running it:

Traceback (most recent call last):
File "c:\xxxx\xxxx\xxxx\xxxx\xxxx.py", line 7, in <module>
data.index = pd.to_datetime(data.Time, format='%Y.%m.%d %H:%M:%S')
File "C:\xxxx\xxxx\xxxx\xxxx\xxxx\xxxx\xxxx\xxxx\xxxx\generic.py", line 5989, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'Time'

the csv file is build like this:
<DATE> <TIME> <BID> <ASK> <LAST> <VOLUME> <FLAGS>
2023.06.01 01:00:07.592 1962.69 1963.08 6
2023.06.01 01:00:07.831 1962.60 1963.05 6
etc.

and my code looks like this:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime

data = pd.read_csv("XAUUSD2020_2023.csv")
data.index = pd.to_datetime(data.Time, format='%Y.%m.%d %H:%M:%S') 
data1hrAsk = data.Ask.resample('60min').ohlc()
data1hrAsk = data1hrAsk.dropna()
train_data = data1hrAsk.close.to_list()
ret = data1hrAsk.close.pct_change()
--rest of the code--

im pretty new to coding in python i really tried everything i could. please help me.:(
Reply
#2
Exactly what the message says,
Error:
AttributeError: 'DataFrame' object has no attribute 'Time'
You do that here:
data.index = pd.to_datetime(data.Time, format='%Y.%m.%d %H:%M:%S') 
Did you mean this:
data.index = pd.to_datetime(data["Time"], format='%Y.%m.%d %H:%M:%S') 
You can also do the conversion when reading the CSV:
data = pd.read_csv(
    "XAUUSD2020_2023.csv",
    parse_dates=["Time"],
    date_format='%Y.%m.%d %H:%M:%S')
Reply
#3
Isn't it likely that you might get a non-unique index with just TIME, especially if you have a lot of data?

Add DATE and TIME as index maybe? (I don't know why you had <DATE> etc. Is that important?)

df.index = pd.to_datetime(df['DATE'] + ' '  + df['TIME'], format='%Y.%m.%d %H:%M:%S.%f')
Then you could forget the columns DATE and TIME, the index is carried over

df2 = df[['BID','ASK','LAST','VOLUME','FLAGS']]
df2

Output:
BID ASK LAST VOLUME FLAGS 2023-06-01 01:00:07.592 1962.69 1963.08 6 100 G 2023-06-02 13:01:07.891 1962.60 1963.05 6 200 B
Reply
#4
(Nov-15-2023, 08:43 PM)MassiJames Wrote: the csv file is build like this:
You should post raw sample of the .csv,so don't have to guess.
So something like this to get a working DataFrame,adjust as needed to your .csv.
stock_data.csv:
Output:
2023.06.01 01:00:07.592 1962.69 1963.08 6 2023.06.01 01:00:07.831 1962.60 1963.05 6 2023.06.01 01:00:08.123 1962.65 1963.10 6 2023.06.01 01:00:08.456 1962.68 1963.12 6
import pandas as pd

df = pd.read_csv("stock_data.csv", sep=' ', names=['Date', 'Time', 'Bid', 'Ask', 'Last'])
df['DateTime'] = pd.to_datetime(df['Date'] + ' ' + df['Time'])
df.set_index('DateTime', inplace=True)
df = df.drop(columns=['Date', 'Time'])
df = df.reset_index()
Test,look at DataFrame and check that types are ok.
>>> df
                 DateTime      Bid      Ask  Last
0 2023-06-01 01:00:07.592  1962.69  1963.08     6
1 2023-06-01 01:00:07.831  1962.60  1963.05     6
2 2023-06-01 01:00:08.123  1962.65  1963.10     6
3 2023-06-01 01:00:08.456  1962.68  1963.12     6

>>> df.dtypes
DateTime    datetime64[ns]
Bid                float64
Ask                float64
Last                 int64
dtype: object
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading a file name fron a folder on my desktop Fiona 4 928 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,116 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 1,107 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 996 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 901 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 996 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Failing reading a file and cannot exit it... tester_V 8 1,838 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Upgrading from 2 to 3 and having file write problems KenHorse 2 1,499 May-08-2022, 09:47 PM
Last Post: KenHorse
  Reading .csv file doug2019 4 1,710 Apr-29-2022, 09:55 PM
Last Post: deanhystad
  Reading an Input File DaveG 1 1,260 Mar-27-2022, 02:08 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020