Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alignment of complex numbers #2756

Merged
merged 9 commits into from
May 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/abstractdataframe/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,25 @@ function _show(io::IO,
# floating points.
alignment_anchor_regex = Dict{Int, Vector{Regex}}()

# Columns composed of numbers are printed aligned to the right.
alignment_regex_vec = [r"\."]
# Regex to align real numbers.
alignment_regex_real = [r"\."]

# Regex for columns with complex numbers.
#
# Here we are matching `+` or `-` unless it is not at the beginning of the
# string or an `e` precedes it.
alignment_regex_complex = [r"(?<!^)(?<!e)[+-]"]

for i = 1:num_cols
type_i = nonmissingtype(types[i])

if type_i <: Number
alignment_anchor_regex[i] = alignment_regex_vec
if type_i <: Complex
alignment_anchor_regex[i] = alignment_regex_complex
alignment[i] = :r
elseif type_i <: Real
alignment_anchor_regex[i] = alignment_regex_real
alignment[i] = :r
elseif type_i <: Number
alignment[i] = :r
end
end
Expand Down
46 changes: 46 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,52 @@ end

end

@testset "Complex number alignment" begin
a = Union{Missing, Number}[
im,
1 + im,
-1 - im,
1.123 + 123im,
-2.323 - 32im,
-1//3 - 4//5im,
1028.23123,
missing,
-0.304105,
2123123,
+0.304105,
1.3123e-10 + 1.123e-5im
]
b = Int64[i ≤ 6 ? -1 * 10^(i - 1) : 10^(i - 7) for i = 1:12]
c = Union{Missing, Float64}[i == 5 ? missing : 10.0^(i-4) for i = 1:12]
d = ComplexF64[(-1)^(i % 3 == 0) * 10.0^(i-6) + (-1)^i * (10.0)^(11-i-6) * im for i = 1:12]
df = DataFrame(very_big_column_name_1 = a,
very_big_column_name_2 = b,
very_big_column_name_3 = c,
very_big_column_name_4 = d)

io = IOContext(IOBuffer())
show(io, df)
str = String(take!(io.io))

@test str == """
12×4 DataFrame
Row │ very_big_column_name_1 very_big_column_name_2 very_big_column_name_3 very_big_column_name_4
│ Union{Missing, Number} Int64 Float64? $(Complex{Float64})
─────┼────────────────────────────────────────────────────────────────────────────────────────────────
1 │ im -1 0.001 1.0e-5-10000.0im
2 │ 1+1im -10 0.01 0.0001+1000.0im
3 │ -1-1im -100 0.1 -0.001-100.0im
4 │ 1.123+123.0im -1000 1.0 0.01+10.0im
5 │ -2.323-32.0im -10000 missing 0.1-1.0im
6 │ -1//3+4//5*im -100000 100.0 -1.0+0.1im
7 │ 1028.23 1 1000.0 10.0-0.01im
8 │ missing 10 10000.0 100.0+0.001im
9 │ -0.304105 100 100000.0 -1000.0-0.0001im
10 │ 2123123 1000 1.0e6 10000.0+1.0e-5im
11 │ 0.304105 10000 1.0e7 100000.0-1.0e-6im
12 │ 1.3123e-10+1.123e-5im 100000 1.0e8 -1.0e6+1.0e-7im"""
end
ronisbr marked this conversation as resolved.
Show resolved Hide resolved

@testset "Issue #2673 - Vertical line when not showing row numbers" begin
df = DataFrame(a = Int64[10, 20], b = Int64[30, 40], c = Int64[50, 60])

Expand Down