1

I'm having an issue figuring out how to get data from a dataframe in this format: enter image description here

These are the types that get printed from each column:

DataType[Any, Any, Any, Any, Any]

This is the error i'm getting and already tried converting the types without success:

ERROR: MethodError: no method matching Plots.OHLC(::Vector{Any}, ::Vector{Any}, ::Vector{Any}, ::Vector{Any}, ::Vector{Any})

The code tries to get the data from an excel file and plot it into a candlestick chart:

using Dates
using XLSX
using Plots
using DataFrames

# Load data from Excel file
data = DataFrame(XLSX.readtable("Janeiro2023.xlsx", "Sheet1"))

# Check data types of columns
println(eltype.(eachcol(data)))

# Extract the columns as arrays
datetime_array = data[!, 1]
open_array = data[!, 2]
high_array = data[!, 3]
low_array = data[!, 4]
close_array = data[!, 5]

# Create an OHLC object from the arrays
ohlc = Plots.OHLC(datetime_array, open_array, high_array, low_array, close_array)

# Plot the candlestick chart
plot(ohlc)
Canichim
  • 21
  • 4

1 Answers1

1

You should read your data with infer_eltypes=true when reading excel:

data = DataFrame(XLSX.readtable("Janeiro2023.xlsx", "Sheet1"; infer_eltypes=true))

This will set the appropriate types. However (depending on your input data) the date column will still be a string and you will need to parse it using Dates:

data.Data .= DateTime.(data.Data, dateformat"yyyy-mm-ddTHH:MM:SS")
Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • The infer_eltypes attribute successfully converted the columns to Float64 but the Data column which should be a Date still not passing. Any idea? ERROR: MethodError: no method matching Int64(::Vector{Dates.DateTime}) – Canichim Mar 11 '23 at 00:00
  • In that case just convert it it string before converting to date so the code will be: `data.Data .= DateTime.(string.(data.Data), dateformat"yyyy-mm-ddTHH:MM:SS")` – Przemyslaw Szufel Mar 11 '23 at 14:09