Class: GtfsDf::BaseGtfsTable
- Inherits:
-
Object
- Object
- GtfsDf::BaseGtfsTable
show all
- Defined in:
- lib/gtfs_df/base_gtfs_table.rb
Direct Known Subclasses
Schema::Agency, Schema::Areas, Schema::Attributions, Schema::BookingRules, Schema::Calendar, Schema::CalendarDates, Schema::FareAttributes, Schema::FareLegJoinRules, Schema::FareLegRules, Schema::FareMedia, Schema::FareProducts, Schema::FareRules, Schema::FareTransferRules, Schema::FeedInfo, Schema::Frequencies, Schema::Levels, Schema::LocationGroupStops, Schema::LocationGroups, Schema::Networks, Schema::Pathways, Schema::RiderCategories, Schema::RouteNetworks, Schema::Routes, Schema::Shapes, Schema::StopAreas, Schema::StopAttributes, Schema::StopTimes, Schema::Stops, Schema::Transfers, Schema::Translations, Schema::Trips
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of BaseGtfsTable.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/gtfs_df/base_gtfs_table.rb', line 8
def initialize(input)
@df =
if input.is_a?(Polars::DataFrame)
input
elsif input.is_a?(String)
df = Polars.read_csv(input, infer_schema_length: 0, encoding: "utf8-lossy")
.rename(->(col) { col.strip })
df = df.filter(Polars.all_horizontal(Polars.all.is_null).is_not)
dtypes = self.class::SCHEMA.slice(*df.columns)
df
.with_columns(dtypes.keys.map do |col|
stripped = Polars.col(col).str.strip_chars
Polars.when(stripped.str.len_chars.gt(0))
.then(stripped)
.otherwise(Polars.lit(nil))
end)
.with_columns(dtypes.map do |name, type|
Polars.col(name).cast(type)
end)
else
throw GtfsDf::Error, "Unrecognized input"
end
@validator = SchemaValidator.new(@df, self.class)
end
|
Instance Attribute Details
#df ⇒ Object
Returns the value of attribute df.
5
6
7
|
# File 'lib/gtfs_df/base_gtfs_table.rb', line 5
def df
@df
end
|
#validator ⇒ Object
Returns the value of attribute validator.
5
6
7
|
# File 'lib/gtfs_df/base_gtfs_table.rb', line 5
def validator
@validator
end
|
Class Method Details
.empty_dataframe ⇒ Object
59
60
61
62
63
64
|
# File 'lib/gtfs_df/base_gtfs_table.rb', line 59
def self.empty_dataframe
Polars::DataFrame.new(
const_get(:REQUIRED_FIELDS).map { |field| [field, []] }.to_h,
schema_overrides: const_get(:SCHEMA)
)
end
|
.time_fields ⇒ Object
43
44
45
|
# File 'lib/gtfs_df/base_gtfs_table.rb', line 43
def self.time_fields
const_defined?(:TIME_FIELDS) ? const_get(:TIME_FIELDS) : []
end
|
Instance Method Details
#dataframe ⇒ Object
55
56
57
|
# File 'lib/gtfs_df/base_gtfs_table.rb', line 55
def dataframe
@df
end
|
#errors ⇒ Object
51
52
53
|
# File 'lib/gtfs_df/base_gtfs_table.rb', line 51
def errors
@validator.errors
end
|
#fields ⇒ Object
39
40
41
|
# File 'lib/gtfs_df/base_gtfs_table.rb', line 39
def fields
self.class::SCHEMA.keys
end
|
#valid? ⇒ Boolean
47
48
49
|
# File 'lib/gtfs_df/base_gtfs_table.rb', line 47
def valid?
@validator.valid?
end
|