OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Extract Dates and hours from .CSV file with pandas

  • Thread starter Thread starter Fabian.GLS
  • Start date Start date
F

Fabian.GLS

Guest
Bassically what im trying to do is detect the days inside a .CSV file, and once this days have been extracted, display hour by hour OR from a specific hour to other hour.

What i did for extracting Date Days was:

Code:
import pandas
from tkinter import filedialog as ASK
from tkinter import messagebox
import datetime as dt
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import matplotlib.dates as mdates


class Presion:
def __init__(self):
    self.Doc1 = None
    self.Doc2 = None

    self.excel1 = None
    self.excel2 = None

    self.DiasDisponibles1 = None
    self.DiasDisponibles2 = None

    self.DayList1 = []
    self.DayList2 = []

    self.Plot1X = []
    self.Plot1Y = []

    self.Plot2X = []
    self.Plot2Y = []

    self.DayEndsValue1 = []
    self.DayEndsValue2 = []
    pass


def ReadPresion1(self):
    self.Doc1 = ASK.askopenfilename(initialdir=" /", title="SELECT .CSV FILE!!", filetypes=(("files", "*.csv"), ("all files", "*.*")))

    try:
        self.excel1 = pandas.read_csv(self.Doc1, sep="\t", encoding="utf-16")
    except FileNotFoundError:
        messagebox.showerror(title="INVALID!", message="Make sure select .CSV FILES!")
    else:
        self.excel1.fillna(0, inplace=True)
        self.excel1.columns = ["Hour", "Date", "Value"]

        Last_Date = None
        DayEnds = 0

        for Date in self.excel1.loc[:,"Date"]:
            DayEnds += 1
            if Date != Last_Date:
                self.DayList1.append(Date)
                self.DayEndsValue1.append(DayEnds)
            Last_Date = Date

        self.DayEndsValue1.append(DayEnds)
        print(self.DayList1)
        print(self.DayEndsValue1)

        for Hour in range(self.DayEndsValue1[0], self.DayEndsValue1[1]):
            ValueX = self.excel1.loc[Hour, "Hour"]
            self.Plot1X.append(ValueX)

        for Value in range(self.DayEndsValue1[0], self.DayEndsValue1[1]):
            ValueY = self.excel1.loc[Value, "Value"]
            self.Plot1Y.append(ValueY)



        # Figura1Presion = Figure(figsize=(7,6), dpi=100)
        # Figura1Presion.add_subplot(111).plot(self.Plot1X, self.Plot1Y)
        Figura1ASDF = plt.figure(figsize=(12,7))
        Figura1ASDF.add_subplot(111).plot(self.Plot1X, self.Plot1Y)



        locator = mdates.AutoDateLocator(minticks=12, maxticks=24)
        plt.gcf().axes[0].xaxis.set_major_locator(locator)
        plt.xticks(rotation=45)
        plt.gcf().autofmt_xdate()




        return Figura1ASDF



def getDaysP1(self):
    return self.DayList1

and then in my main script i have a set up tkinter program running where i set up some button to ask .CSV file, etc Here some functionallity:

Code:
def showPlotPresion1():
Figure1 = OBJ_PRESION.ReadPresion1()

DaysP1 = OBJ_PRESION.getDaysP1()
showDaysPresion1(DaysP1)

canvas = FigureCanvasTkAgg(Figure1, master=WindowPresion)  # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)

toolbar = NavigationToolbar2Tk(canvas, WindowPresion)
toolbar.update()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)


def showDaysPresion1(ListPresion1):

optionsSelect = StringVar()
optionsSelect.set("04/26/2021")

TextP1 = Label(WindowPresion, text="Dias Disponibles Presion 1 -->")
TextP1.pack(anchor="w", side=LEFT)

List1 = OptionMenu(WindowPresion, optionsSelect, *ListPresion1)
List1.pack(anchor="w", side=LEFT)

QuickUI How do i extract available hours from this readed days? Here is the csv format: CSV FILE

Thanks in advance for your help!
<p>Bassically what im trying to do is detect the days inside a .CSV file, and once this days have been extracted, display hour by hour OR from a specific hour to other hour.</p>
<p>What i did for extracting Date Days was:</p>
<pre><code>import pandas
from tkinter import filedialog as ASK
from tkinter import messagebox
import datetime as dt
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import matplotlib.dates as mdates


class Presion:
def __init__(self):
self.Doc1 = None
self.Doc2 = None

self.excel1 = None
self.excel2 = None

self.DiasDisponibles1 = None
self.DiasDisponibles2 = None

self.DayList1 = []
self.DayList2 = []

self.Plot1X = []
self.Plot1Y = []

self.Plot2X = []
self.Plot2Y = []

self.DayEndsValue1 = []
self.DayEndsValue2 = []
pass


def ReadPresion1(self):
self.Doc1 = ASK.askopenfilename(initialdir=" /", title="SELECT .CSV FILE!!", filetypes=(("files", "*.csv"), ("all files", "*.*")))

try:
self.excel1 = pandas.read_csv(self.Doc1, sep="\t", encoding="utf-16")
except FileNotFoundError:
messagebox.showerror(title="INVALID!", message="Make sure select .CSV FILES!")
else:
self.excel1.fillna(0, inplace=True)
self.excel1.columns = ["Hour", "Date", "Value"]

Last_Date = None
DayEnds = 0

for Date in self.excel1.loc[:,"Date"]:
DayEnds += 1
if Date != Last_Date:
self.DayList1.append(Date)
self.DayEndsValue1.append(DayEnds)
Last_Date = Date

self.DayEndsValue1.append(DayEnds)
print(self.DayList1)
print(self.DayEndsValue1)

for Hour in range(self.DayEndsValue1[0], self.DayEndsValue1[1]):
ValueX = self.excel1.loc[Hour, "Hour"]
self.Plot1X.append(ValueX)

for Value in range(self.DayEndsValue1[0], self.DayEndsValue1[1]):
ValueY = self.excel1.loc[Value, "Value"]
self.Plot1Y.append(ValueY)



# Figura1Presion = Figure(figsize=(7,6), dpi=100)
# Figura1Presion.add_subplot(111).plot(self.Plot1X, self.Plot1Y)
Figura1ASDF = plt.figure(figsize=(12,7))
Figura1ASDF.add_subplot(111).plot(self.Plot1X, self.Plot1Y)



locator = mdates.AutoDateLocator(minticks=12, maxticks=24)
plt.gcf().axes[0].xaxis.set_major_locator(locator)
plt.xticks(rotation=45)
plt.gcf().autofmt_xdate()




return Figura1ASDF



def getDaysP1(self):
return self.DayList1
</code></pre>
<p>and then in my main script i have a set up tkinter program running where i set up some button to ask .CSV file, etc
Here some functionallity:</p>
<pre><code>def showPlotPresion1():
Figure1 = OBJ_PRESION.ReadPresion1()

DaysP1 = OBJ_PRESION.getDaysP1()
showDaysPresion1(DaysP1)

canvas = FigureCanvasTkAgg(Figure1, master=WindowPresion) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)

toolbar = NavigationToolbar2Tk(canvas, WindowPresion)
toolbar.update()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)


def showDaysPresion1(ListPresion1):

optionsSelect = StringVar()
optionsSelect.set("04/26/2021")

TextP1 = Label(WindowPresion, text="Dias Disponibles Presion 1 -->")
TextP1.pack(anchor="w", side=LEFT)

List1 = OptionMenu(WindowPresion, optionsSelect, *ListPresion1)
List1.pack(anchor="w", side=LEFT)
</code></pre>
<p><a href="https://i.sstatic.net/ZLTI3uVm.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ZLTI3uVm.png" alt="QuickUI" /></a>
How do i extract available hours from this readed days?
Here is the csv format:
<a href="https://drive.google.com/drive/folders/1GnLDiLTzA2O2gHsA-fRhQB8csUQ1dD0B?usp=sharing" rel="nofollow noreferrer">CSV FILE</a></p>
<p>Thanks in advance for your help!</p>
 

Latest posts

Top