@@ -18,17 +18,18 @@ use Package::Stash;
18
18
use Ref::Util qw( is_plain_arrayref is_plain_hashref) ;
19
19
use Scalar::Util qw( openhandle looks_like_number) ;
20
20
use Type::Params;
21
- use Types::Standard qw( Any ArrayRef CodeRef HashRef Maybe Str) ;
21
+ use Types::Standard qw( Any ArrayRef CodeRef Enum HashRef Map Maybe Str) ;
22
22
use Types::PDL qw( Piddle) ;
23
23
use Text::CSV;
24
24
25
25
use Data::Frame::Util qw( guess_and_convert_to_pdl) ;
26
+ use Data::Frame::Types qw( DataType) ;
26
27
27
28
=method from_csv
28
29
29
30
from_csv($file, :$header=true, :$sep=',', :$quote='"',
30
31
:$na=[qw(NA BAD)], :$col_names=undef, :$row_names=undef,
31
- HashRef :$col_types ={},
32
+ Map[Str, DataType] :$dtype ={},
32
33
:$strings_as_factors=false)
33
34
34
35
Create a data frame object from a CSV file. For example,
@@ -40,17 +41,17 @@ Some of the parameters are explained below,
40
41
=for :list
41
42
* C<$file > can be a file name string, a Path::Tiny object, or an opened file
42
43
handle.
43
- * C<$col_types > is a hashref associating column names to their types. Types
44
+ * C<$dtype > is a hashref associating column names to their types. Types
44
45
can be the PDL type names like C<"long" > , C<"double" > , or names of some PDL's
45
46
derived class like C<"PDL::SV" > , C<"PDL::Factor" > , C<"PDL::DateTime" > . If a
46
- column is not specified in C<$col_types > , its type would be automatically
47
+ column is not specified in C<$dtype > , its type would be automatically
47
48
decided.
48
49
49
50
=cut
50
51
51
52
classmethod from_csv ($file , :$header =true, :$sep =" ," , :$quote =' "' ,
52
53
:$na =[qw( NA BAD) ], :$col_names =undef , :$row_names =undef ,
53
- HashRef : $col_types ={},
54
+ Map[Str, DataType] : $dtype ={},
54
55
:$strings_as_factors =false
55
56
) {
56
57
state $check = Type::Params::compile(
@@ -134,32 +135,32 @@ classmethod from_csv ($file, :$header=true, :$sep=",", :$quote='"',
134
135
}
135
136
}
136
137
137
- my $pdl_types = $class -> _pdl_types;
138
+ state $additional_type_to_piddle = {
139
+ datetime => sub { PDL::DateTime-> new_from_datetime($_ [0]) },
140
+ factor => sub { PDL::Factor-> new($_ [0]) },
141
+ logical => sub { PDL::Logical-> new($_ [0]) },
142
+ };
138
143
my $package_pdl_core = Package::Stash-> new(' PDL::Core' );
139
144
my $to_piddle = sub {
140
145
my ($name ) = @_ ;
141
146
my $x = $columns {$name };
142
147
143
- if (my $col_type = $col_types -> {$name }) {
144
- if (elem($col_type , $pdl_types )) {
145
- my $f = $package_pdl_core -> get_symbol(" &$col_type " );
146
- return $f -> ($x ) if $f ;
147
- }
148
- if ($col_type =~ / ^PDL::(?:Factor|SV|DateTime)$ / ) {
149
- if ($col_type eq ' PDL::DateTime' ) {
150
- return $col_type -> new_from_datetime($x );
151
- } else {
152
- return $col_type -> new($x );
153
- }
148
+ if ( my $type = $dtype -> {$name } ) {
149
+ my $f_new = $additional_type_to_piddle -> {$type }
150
+ // $package_pdl_core -> get_symbol(" &$type " );
151
+ if ($f_new ) {
152
+ return $f_new -> ($x );
154
153
}
155
-
156
- die " Invalid column type '$col_type '" ;
157
- } else {
154
+ else {
155
+ die " Invalid data type '$type '" ;
156
+ }
157
+ }
158
+ else {
158
159
return guess_and_convert_to_pdl(
159
160
$x ,
160
161
na => $na ,
161
162
strings_as_factors => $strings_as_factors
162
- );
163
+ );
163
164
}
164
165
};
165
166
@@ -222,12 +223,6 @@ method to_csv ($file, :$sep=',', :$quote='"', :$na='NA',
222
223
}
223
224
}
224
225
225
- classmethod _pdl_types () {
226
- state $types = [ map { PDL::Types::typefld( $_ , ' ppforcetype' ); }
227
- PDL::Types::typesrtkeys() ];
228
- return $types ;
229
- }
230
-
231
226
1;
232
227
233
228
__END__
0 commit comments