Methoderror: no method matching isless when using sort() on Dataframe in Julia
Methoderror: no method matching isless when using sort() on Dataframe in Julia
我有一个 excel 文件,我将其读入 Dataframe。
using DataFrames, XLSX
df = DataFrame(XLSX.readtable("Stocks/WIHL-Wihlborgs.xlsx", "PriceMonth")...)
│ Row │ Date │ Openprice │ Highprice │ Lowprice │ Closeprice │ Volume │
│ │ Any │ Any │ Any │ Any │ Any │ Any │
├─────┼────────────┼───────────┼───────────┼──────────┼────────────┼──────────┤
│ 1 │ 2020-12-23 │ 189.1 │ 189.7 │ 170.3 │ 181.5 │ 4170122 │
│ 2 │ 2020-11-30 │ 160.0 │ 191.6 │ 158.0 │ 189.1 │ 8006506 │
│ 3 │ 2020-10-30 │ 178.4 │ 184.3 │ 151.2 │ 160.6 │ 6760931 │
│ 4 │ 2020-09-30 │ 138.5 │ 178.8 │ 137.9 │ 177.5 │ 9005351 │
│ 5 │ 2020-08-31 │ 147.2 │ 152.1 │ 137.2 │ 138.3 │ 4865386 │
sort(df)
但是当我尝试在此 DateFrame 上使用 sort()
函数时,我得到 LoadError: MethodError: no method matching isless(::String, ::Dates.Date)
我认为这很奇怪,因为我在具有 prices/day 的同一个 excel 文件中有另一个选项卡,并且可以正常排序。我看到的唯一区别是,在 Excel 文件中,prices/month 选项卡的日期字段为日期,而 prices/day 选项卡的日期字段为任意。但是在 DataFrame 中,两个选项卡的 Date 字段都是 Any.
我很难理解为什么 prices/day 选项卡可以与 sort()
一起使用而 prices/month 选项卡却不能,而是给了我 LoadError
。
将列更改为适当的数据类型应该可以解决此问题 (infer_eltypes):
using DataFrames, XLSX
df = DataFrame(XLSX.readtable("./dat.xlsx", "Sheet1", infer_eltypes=true)..., )
#5×6 DataFrame
# Row │ Date Openprice Highprice Lowprice Closeprice Volume
# │ Date Any Float64 Any Float64 Int64
#─────┼─────────────────────────────────────────────────────────────────
# 1 │ 2020-12-23 189.1 189.7 170.3 181.5 4170122
# 2 │ 2020-11-30 160 191.6 158 189.1 8006506
# 3 │ 2020-10-30 178.4 184.3 151.2 160.6 6760931
# 4 │ 2020-09-30 138.5 178.8 137.9 177.5 9005351
# 5 │ 2020-08-31 147.2 152.1 137.2 138.3 4865386
现在,日期排序工作正常:
df[sortperm(df.Date),1:2]
#5×2 DataFrame
# Row │ Date Openprice
# │ Date Any
#─────┼───────────────────────
# 1 │ 2020-08-31 147.2
# 2 │ 2020-09-30 138.5
# 3 │ 2020-10-30 178.4
# 4 │ 2020-11-30 160
# 5 │ 2020-12-23 189.1
也可以手动设置日期类型:
using Dates
df.Date = Dates.Date.(string.(df.Date), "yyyy-mm-dd")
#5-element Array{Date,1}:
# 2020-12-23
# 2020-11-30
# 2020-10-30
# 2020-09-30
# 2020-08-31
我有一个 excel 文件,我将其读入 Dataframe。
using DataFrames, XLSX
df = DataFrame(XLSX.readtable("Stocks/WIHL-Wihlborgs.xlsx", "PriceMonth")...)
│ Row │ Date │ Openprice │ Highprice │ Lowprice │ Closeprice │ Volume │
│ │ Any │ Any │ Any │ Any │ Any │ Any │
├─────┼────────────┼───────────┼───────────┼──────────┼────────────┼──────────┤
│ 1 │ 2020-12-23 │ 189.1 │ 189.7 │ 170.3 │ 181.5 │ 4170122 │
│ 2 │ 2020-11-30 │ 160.0 │ 191.6 │ 158.0 │ 189.1 │ 8006506 │
│ 3 │ 2020-10-30 │ 178.4 │ 184.3 │ 151.2 │ 160.6 │ 6760931 │
│ 4 │ 2020-09-30 │ 138.5 │ 178.8 │ 137.9 │ 177.5 │ 9005351 │
│ 5 │ 2020-08-31 │ 147.2 │ 152.1 │ 137.2 │ 138.3 │ 4865386 │
sort(df)
但是当我尝试在此 DateFrame 上使用 sort()
函数时,我得到 LoadError: MethodError: no method matching isless(::String, ::Dates.Date)
我认为这很奇怪,因为我在具有 prices/day 的同一个 excel 文件中有另一个选项卡,并且可以正常排序。我看到的唯一区别是,在 Excel 文件中,prices/month 选项卡的日期字段为日期,而 prices/day 选项卡的日期字段为任意。但是在 DataFrame 中,两个选项卡的 Date 字段都是 Any.
我很难理解为什么 prices/day 选项卡可以与 sort()
一起使用而 prices/month 选项卡却不能,而是给了我 LoadError
。
将列更改为适当的数据类型应该可以解决此问题 (infer_eltypes):
using DataFrames, XLSX
df = DataFrame(XLSX.readtable("./dat.xlsx", "Sheet1", infer_eltypes=true)..., )
#5×6 DataFrame
# Row │ Date Openprice Highprice Lowprice Closeprice Volume
# │ Date Any Float64 Any Float64 Int64
#─────┼─────────────────────────────────────────────────────────────────
# 1 │ 2020-12-23 189.1 189.7 170.3 181.5 4170122
# 2 │ 2020-11-30 160 191.6 158 189.1 8006506
# 3 │ 2020-10-30 178.4 184.3 151.2 160.6 6760931
# 4 │ 2020-09-30 138.5 178.8 137.9 177.5 9005351
# 5 │ 2020-08-31 147.2 152.1 137.2 138.3 4865386
现在,日期排序工作正常:
df[sortperm(df.Date),1:2]
#5×2 DataFrame
# Row │ Date Openprice
# │ Date Any
#─────┼───────────────────────
# 1 │ 2020-08-31 147.2
# 2 │ 2020-09-30 138.5
# 3 │ 2020-10-30 178.4
# 4 │ 2020-11-30 160
# 5 │ 2020-12-23 189.1
也可以手动设置日期类型:
using Dates
df.Date = Dates.Date.(string.(df.Date), "yyyy-mm-dd")
#5-element Array{Date,1}:
# 2020-12-23
# 2020-11-30
# 2020-10-30
# 2020-09-30
# 2020-08-31