<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Import Excel or CSV To Pandas
This file covers the process of importing excel and csv files into a pandas dataframe. Note
Step1:  Step 2
Now lets create a variable, <code>filePath</code>, that is a string containing the full path to the file we want to import. The code below looks in the current working directory for the file given a file name input by the user. This isn't necessary, and is just included for convienence. Alternatively, user can input a full path into the <code>filePath</code> variable.
Step2:  Step 3
Great! Now lets read the data into a dataframe called <code>df</code>.
This will allow our data to be accessible by the string in the header.
Step3:  Our data is now accessible by a key value. The keys are the column headers in the dataframe. In this example case, those are 'Time (s) - Dev1/ai0' and  'Temperature - Dev1/ai0'. For example, lets access the data in the first column.
Step4:  What would happen if we tried to access the data with an invalid key, say <code>1</code> for example? Lets try it to find out.
Note
Step5:  So lets say you have a large dataframe with unknown columns. There is a simple way to index them without having prior knowledge of what the dataframe columns are. Namely, the <code>columns</code> method in pandas.
Step6:  Data Manipulation (Plots)
Now that we have the data easily accessible in python, lets look at how to plot it. <code>Pandas</code> allows you to use matplotlib to plot, however it is done using methods built into pandas.
Although the methods to create an manipulate plots are built into <code>Pandas</code>, we will still have to import matplotlib to save and show the plots.
Step7:  In order to demonstrate the plotting capabilities of pandas arrays, lets use the example data that we imported earlier. The data frame contains only the two columns that were in the file; temperature and time. Because of this simplicity, we can trust pandas to properly interpret the first column as time and the second column as th measurement (temperature). Thus we can plot with the simple command.
<code>df.plot()</code>
Step8:  While this simplification is nice, it is generally better to specify what data you want to plot. Particularly if you are automating the plotting of a large set of dataframes. To do this, specify the <code>x</code> and <code>y</code> arrays in your dataframe as you would in a standard <code>matplotlib</code> plot call, however since this plotting function is a method of the dataframe, you need only specify the column.
I.e.
Step9:  Now that we have the basics down, lets spice up the plot a little bit.
Step10:  Data Manipulation (Timestamps)
One thing you probably noticed in these plots is that the time axis isn't all that useful. It would be better to change the timestamps to a more useful form like seconds since start. Lets go through the process of making that conversion.
First, lets see what the timestamp currently looks like.
Step11:  Good news! Since python interpreted the date as a datetime object, we can use datetime object methods to determine the time in seconds. The one caveat is that we can only determine a time difference, not an absolute time. For more on this, read this stackoverflow question.
The first thing we have to do is convert these <code>datetime.time</code> objects into <code>datetime.datetime</code> objects using <code>datetime.combine</code>
Note
Step12:  Note
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
import os
Explanation: Import Excel or CSV To Pandas
This file covers the process of importing excel and csv files into a pandas dataframe. Note: the methods for importing excel and csv files is almost identical. The major difference is in the method used. This notebook serves as a tutorial for both.
Importing Excel (xlsx): <br>
The function used is read_excel. <br>
Importing comma separated values (csv): <br>
The function used is read_csv. <br>
Step 1
Lets start by importing pandas and os. We will be using pandas to create a dataframe from our data, and os to get file paths.
End of explanation
dirPath = os.path.realpath('.')
fileName = 'assets/coolingExample.xlsx'
filePath = os.path.join(dirPath, fileName)
Explanation: Step 2
Now lets create a variable, <code>filePath</code>, that is a string containing the full path to the file we want to import. The code below looks in the current working directory for the file given a file name input by the user. This isn't necessary, and is just included for convienence. Alternatively, user can input a full path into the <code>filePath</code> variable.
End of explanation
df = pd.read_excel(filePath,header=0)
df.head()
Explanation: Step 3
Great! Now lets read the data into a dataframe called <code>df</code>.
This will allow our data to be accessible by the string in the header.
End of explanation
df[df.columns[0]]
Explanation: Our data is now accessible by a key value. The keys are the column headers in the dataframe. In this example case, those are 'Time (s) - Dev1/ai0' and  'Temperature - Dev1/ai0'. For example, lets access the data in the first column.
End of explanation
try:
    df[1]
except KeyError:
    print("KeyError: 1 - not a valid key")
Explanation: What would happen if we tried to access the data with an invalid key, say <code>1</code> for example? Lets try it to find out.
Note: I enclose this code in a <code>try: except:</code> statement in order to prevent a huge error from being generated.
End of explanation
cols = df.columns
for col in cols:
    print(df[col])
Explanation: So lets say you have a large dataframe with unknown columns. There is a simple way to index them without having prior knowledge of what the dataframe columns are. Namely, the <code>columns</code> method in pandas.
End of explanation
import matplotlib.pyplot as plt
Explanation: Data Manipulation (Plots)
Now that we have the data easily accessible in python, lets look at how to plot it. <code>Pandas</code> allows you to use matplotlib to plot, however it is done using methods built into pandas.
Although the methods to create an manipulate plots are built into <code>Pandas</code>, we will still have to import matplotlib to save and show the plots.
End of explanation
plt.figure(1)
ax = df.plot()
plt.show()
Explanation: In order to demonstrate the plotting capabilities of pandas arrays, lets use the example data that we imported earlier. The data frame contains only the two columns that were in the file; temperature and time. Because of this simplicity, we can trust pandas to properly interpret the first column as time and the second column as th measurement (temperature). Thus we can plot with the simple command.
<code>df.plot()</code>
End of explanation
plt.figure(2)
ax = df.plot(cols[0],cols[1])
plt.show()
Explanation: While this simplification is nice, it is generally better to specify what data you want to plot. Particularly if you are automating the plotting of a large set of dataframes. To do this, specify the <code>x</code> and <code>y</code> arrays in your dataframe as you would in a standard <code>matplotlib</code> plot call, however since this plotting function is a method of the dataframe, you need only specify the column.
I.e.
End of explanation
plt.figure(3)
ax = df.plot(cols[0],cols[1])
ax.set_title('This is a Title')
ax.set_ylabel('Temperature (deg F)')
ax.grid()
plt.show()
Explanation: Now that we have the basics down, lets spice up the plot a little bit.
End of explanation
df[cols[0]][0]
Explanation: Data Manipulation (Timestamps)
One thing you probably noticed in these plots is that the time axis isn't all that useful. It would be better to change the timestamps to a more useful form like seconds since start. Lets go through the process of making that conversion.
First, lets see what the timestamp currently looks like.
End of explanation
from datetime import datetime, date
startTime = df[cols[0]][0]
timeArray = []
for i in range(0,len(df[cols[0]])):
    timeArray.append((datetime.combine(date.today(), df[cols[0]][i]) - datetime.combine(date.today(), startTime)).total_seconds())
Explanation: Good news! Since python interpreted the date as a datetime object, we can use datetime object methods to determine the time in seconds. The one caveat is that we can only determine a time difference, not an absolute time. For more on this, read this stackoverflow question.
The first thing we have to do is convert these <code>datetime.time</code> objects into <code>datetime.datetime</code> objects using <code>datetime.combine</code>
Note: importing datetime is a little weird.. <code>datetime</code> is both a module and a class.
End of explanation
plt.figure(4)
plt.plot(timeArray, df[cols[1]], 'b')
plt.title('This is a graph with a better time axis')
plt.ylabel('Temperature (deg F)')
plt.xlabel('Time (s)')
plt.grid()
plt.show()
Explanation: Note: There is probably a better way of doing this (i.e. without a loop, but I'm tired and can't think of anything right now)
End of explanation
<END_TASK>