Skip to content
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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "1.5.0"
[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
DataValues = "e7dc6d0d-1eca-5fa6-8ad6-5aecde8b7ea5"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
IteratorInterfaceExtensions = "82899510-4779-5014-852e-03e436cf321d"
Lazy = "50d2b5c4-7a5e-59d5-8109-a42b560f39c0"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Expand All @@ -22,5 +23,5 @@ Lazy = "0.15"
PyCall = "1.90"
TableTraits = "^0.4, ^1"
TableTraitsUtils = "^0.3, ^0.4, ^1"
julia = "0.7, 1"
Tables = "1"
julia = "0.7, 1"
21 changes: 20 additions & 1 deletion src/Pandas.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__precompile__(true)
module Pandas

using Dates
using PyCall
using Lazy
using Compat
Expand Down Expand Up @@ -76,9 +77,27 @@ end

quot(x) = Expr(:quote, x)


function convert_datetime_series_to_julia_vector(series)
N = length(series)
out = Array{Dates.DateTime}(undef, N)
for i in 1:N
# PyCall.jl overloads the getindex method on `series` to automatically convert
# to a Julia date type.
out[i] = series[i]
end
return out
end


function Base.Array(x::PandasWrapped)
if typeof(x) <: Series && x.pyo.dtype == np.dtype("<M8[ns]")
return convert_datetime_series_to_julia_vector(x)
end
c = np.asarray(x.pyo)
if typeof(c).parameters[1] == PyObject
# PyCall will automatically try to convert the result of np.asarray to a native Julia array containing native Julia objects.
# If it can't, it will return a PyObject or a Julia vector of PyObjects.
if typeof(c) == PyObject || typeof(c).parameters[1] == PyObject
out = Array{Any}(undef, size(x))
for idx in eachindex(out)
out[idx] = convert(PyAny, c[idx])
Expand Down
4 changes: 3 additions & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DataValues = "e7dc6d0d-1eca-5fa6-8ad6-5aecde8b7ea5"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
IteratorInterfaceExtensions = "82899510-4779-5014-852e-03e436cf321d"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
TableTraits = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
DataFrames = "1"
DataValues = "0.4.4"
IteratorInterfaceExtensions = "^0.1.1, ^1"
TableTraits = "^0.4, ^1"
Tables = "1"
DataFrames = "1"
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Pandas
using Test
import DataFrames
using PyCall
using Dates

df = DataFrame(Dict(:name=>["a", "b"], :age=>[27, 30]))
age = values(df.age)
Expand Down Expand Up @@ -50,3 +52,19 @@ julia_df = DataFrames.DataFrame(x=[1,2], y=[missing, missing])
py_df = Pandas.DataFrame(julia_df)
expected_df = Pandas.DataFrame(:x=>[1,2], :y=>[NaN, NaN])
@test Pandas.equals(py_df, expected_df)

# Issue #68
py"""
import pandas as pd

def get_df():
df = pd.DataFrame({
"a":pd.to_datetime(["2021.01.15","2021.01.15","2020.04.06"])
})
return df
"""

py_df = py"get_df"()|>Pandas.DataFrame
julia_df = DataFrames.DataFrame(py_df)

@test julia_df.a == [DateTime(2021, 1, 15), DateTime(2021, 1, 15), DateTime(2020, 4, 6)]