0

I'm fairly inexperienced with Python and I need some help - I have a csv file with different columns for day, month, year, hours, minutes, seconds and for my measured parameters. I need to combine the date/time information into one column so I can then plot the date/time against the parameter. Can anyone offer any assistance on doing this?

I have searched and found some similar questions but nothing has worked!

Thank you in advance!

My code is as follows:

from matplotlib import pyplot as plt
import numpy as np

import csv
with open('Weather station data 19.03 to 21.03.csv', 'rb') as f:
    reader = csv.reader(f, delimiter=',')
    next(reader, None)
    D = []
    M = []
    Y = []
    Hrs = []
    Mins = []
    Secs = []
    Pres = []
    Temp = []
    RH = []
    Volts = []
    Date_time = []
    for col in reader:
        Day = col[0]
        Month = col[1]
        Year = col[2]
        Hour = col[3]
        Minute = col[4]
        Second = col[5]
        Pressure = col[6]
        Temperature = col[7]
        Humidity = col[8]
        Voltage = col[9]
        D.append(Day)
        M.append(Month)
        Y.append(Year)
        Hrs.append(Hour)
        Mins.append(Minute)
        Secs.append(Secs)
        Pres.append(Pressure)
        Temp.append(Temperature)
        RH.append(Humidity)
        Volts.append(Voltage)

I want to be able to combine the date and the time information together, so I can then plot it against the pressure or temperature or RH

nekomatic
  • 5,988
  • 1
  • 20
  • 27
  • 1
    What are the contents of the CSV? And what have you tried so far? – Reck Mar 21 '18 at 11:09
  • you can combine column values with pandas module – Pyd Mar 21 '18 at 11:12
  • 1
    Can you share the code what you have so far? Can you share the questions you tried so far? – TessavWalstijn Mar 21 '18 at 11:16
  • Hi, the CSV contains columns (in order): Day, Month, Year, Hours, Minutes, Seconds, Pressure, Temperature, Relative Humidity and Supply Voltage. It is data from a weather station, and I wish to plot graphs of temperature against time, pressure against time, etc. – harriet123 Mar 21 '18 at 11:21
  • I have never used Panda's before (haven't been taught them!), but I will try to find the link for the other questions I looked at – harriet123 Mar 21 '18 at 11:22
  • if you import the pandas module then run csv_contents = pandas.read_csv("csv/file/path.csv") and add a subset of the contents to your question, it will help us understand your problem. – tda Mar 21 '18 at 11:23
  • Here is my code:from matplotlib import pyplot as plt import numpy as np import csv with open('Weather station data 19.03 to 21.03.csv', 'rb') as f: reader = csv.reader(f, delimiter=',') next(reader, None) D = [] M = [] Y = [] Hrs = [] Mins = [] Secs = [] Pres = [] Temp = [] RH = [] Volts = [] Date_time = [] – harriet123 Mar 21 '18 at 11:25
  • for col in reader: Day = col[0] Month = col[1] Year = col[2] Hour = col[3] Minute = col[4] Second = col[5] Pressure = col[6] Temperature = col[7] Humidity = col[8] Voltage = col[9] D.append(Day) M.append(Month) Y.append(Year) Hrs.append(Hour) Mins.append(Minute) Secs.append(Secs) Pres.append(Pressure) Temp.append(Temperature) RH.append(Humidity) Volts.append(Voltage) – harriet123 Mar 21 '18 at 11:26
  • I tried using code from this question: https://stackoverflow.com/questions/19350806/how-to-convert-columns-into-one-datetime-column-in-pandas – harriet123 Mar 21 '18 at 11:27
  • @harriet123 Can you edit in your code in the original question, and please add what went wrong. – Pax Vobiscum Mar 21 '18 at 11:28
  • Have added my code into the original question (sorry - am new to this!) – harriet123 Mar 21 '18 at 11:36

1 Answers1

0

It sounds like you want to convert the 6 fields into a datetime object:

import datetime
day, month, year, hour, minute, second = col[0:6]
datetime.datetime(year, month, day, hour, minute, second)

You probably need to convert your strings into numbers at some point too.

Constance
  • 202
  • 2
  • 8
  • Thank you! I have put the code in, and I get this error: "datetime.datetime(day, month, year, hour, minute, second) TypeError: an integer is required" - do you know why? – harriet123 Mar 21 '18 at 11:55
  • As I wrote, you need to convert your strings into numbers: `day, month, year, hour, minute, second = [int(val) for val in col[0:6]]` – Constance Mar 21 '18 at 15:43