1

I have this Excel file data as in the image below enter image description here] following this tutorial with data mentioned (https://i.stack.imgur.com/kbI7C.png) I use colab notebook : by writing code down

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from numpy import nan

from tensorflow.keras import Sequential
from tensorflow.keras.layers import LSTM, Dense

from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
#Reading the dataset
data_path= "/content/data.xlsx"
data = pd.read_excel(data_path)
data.head()

When try to check all data columns type by using data.info() I got :

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 84960 entries, 0 to 84959
Data columns (total 9 columns):
 #   Column           Non-Null Count  Dtype         
---  ------           --------------  -----         
 0   Time             84960 non-null  datetime64[ns]
 1   Fridge           84960 non-null  float64       
 2   Lights           84960 non-null  float64       
 3   Microwave        84960 non-null  float64       
 4   Pump1            84960 non-null  float64       
 5   Pump2            84960 non-null  float64       
 6   TV               84960 non-null  float64       
 7   Washing Machine  84960 non-null  float64       
 8   Total Load       84960 non-null  float64       
dtypes: datetime64[ns](1), float64(8)
memory usage: 5.8 MB

I am trying to convert Time type datetime64 to float64 by

# data = data.astype('float')
x = data['Time'].values.astype("float64")
x

but got this issue :

3632             except TypeError:
   3633                 # If we have a listlike key, _check_indexing_error will raise

KeyError: 'Time'
## What I am expect : 
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 84960 entries, 0 to 84959
Data columns (total 9 columns):
 #   Column           Non-Null Count  Dtype         
---  ------           --------------  -----         
 0   Time             84960 non-null  float64
 1   Fridge           84960 non-null  float64       
 2   Lights           84960 non-null  float64       
 3   Microwave        84960 non-null  float64       
 4   Pump1            84960 non-null  float64       
 5   Pump2            84960 non-null  float64       
 6   TV               84960 non-null  float64       
 7   Washing Machine  84960 non-null  float64       
 8   Total Load       84960 non-null  float64       
dtypes: float64(9)
memory usage: 5.8 MB

The format of Time: dd/mm/yyyy hh:mm enter image description here

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
Mohammed
  • 346
  • 1
  • 12

2 Answers2

1

Try the below to convert:

data['Time'] = data['Time'].apply(lambda x: x.timestamp())
data['Time'] = data['Time'].astype('float64')
data.info()
Abdulmajeed
  • 1,502
  • 2
  • 10
  • 13
  • still has the new issue with same reason : I am sharing notebook her https://colab.research.google.com/drive/15VgkH-hM50l3Xcc0FUdmuGHHK2VpM9SX?usp=sharing – Mohammed Mar 18 '23 at 23:21
  • 1
    can you run this code to ensure that the Time column exist data = pd.read_excel(data_path) print(data.head()) ? Because the error says that the column does not exist – Abdulmajeed Mar 18 '23 at 23:29
  • 1
    yes it exists as you can see in collab when typing data.columns we get Index(['Time ', 'Fridge', 'Lights', 'Microwave', 'Pump1', 'Pump2', 'TV', 'Washing Machine', 'Total Load'], dtype='object') – Mohammed Mar 18 '23 at 23:35
  • 2
    Ok there is possibility that there might extra name in the column name. Do this print(data.columns) get the column name then change via this line data.rename(columns={'Time ': 'Time'}, inplace=True) then try again. See how it goes – Abdulmajeed Mar 19 '23 at 10:30
0

The solution as @Abedulmajeed said there was space with column name Time changed to Time

print(data.columns)
data.rename(columns={'Time ': 'Time'}, inplace=True)
data['Time'] = data['Time'].apply(lambda x: x.timestamp())
data['Time'] = data['Time'].astype('float64')
data.info()
Mohammed
  • 346
  • 1
  • 12