Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
dios
Manage
Activity
Members
Labels
Plan
Issues
11
Issue boards
Milestones
Wiki
Jira
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RDM
dios
Commits
6138cd39
Commit
6138cd39
authored
5 years ago
by
Bert Palm
🎇
Browse files
Options
Downloads
Patches
Plain Diff
WIP - improve itype 2.0
parent
c3f046f4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!2
Develop
,
!1
complete rework
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dios/itypes.py
+51
-41
51 additions, 41 deletions
dios/itypes.py
with
51 additions
and
41 deletions
dios/itypes.py
+
51
−
41
View file @
6138cd39
...
...
@@ -8,92 +8,102 @@ class __Itype:
class
DatetimeItype
(
__Itype
):
name
=
'
datetime
'
unique
=
True
subtypes
=
(
pd
.
DatetimeIndex
,)
class
IntegerItype
(
__Itype
):
name
=
'
integer
'
unique
=
True
subtypes
=
(
pd
.
RangeIndex
,
pd
.
Int64Index
,
pd
.
UInt64Index
,)
class
FloatItype
(
__Itype
):
name
=
'
float
'
subtypes
=
(
pd
.
Float64Index
,)
class
NumericItype
(
__Itype
):
name
=
"
numeric
"
subtypes
=
(
pd
.
RangeIndex
,
pd
.
Int64Index
,
pd
.
UInt64Index
,
pd
.
Float64Index
,)
unique
=
True
class
OtherItype
(
__Itype
):
name
=
"
other
"
subtypes
=
(
pd
.
CategoricalIndex
,
pd
.
IntervalIndex
,
pd
.
PeriodIndex
,)
unique
=
True
# class MultiItype(__Itype):
# name = "multi"
# subtypes = (pd.MultiIndex, )
# unique = ??
class
NumericItype
(
__Itype
):
name
=
"
numeric
"
subtypes
=
(
IntegerItype
.
subtypes
+
FloatItype
.
subtypes
)
unique
=
False
class
MixedItype
(
__Itype
):
name
=
"
mixed
"
subtypes
=
(
pd
.
DatetimeIndex
,
pd
.
RangeIndex
,
pd
.
Int64Index
,
pd
.
UInt64Index
,
pd
.
RangeIndex
,
pd
.
CategoricalIndex
,
pd
.
IntervalIndex
,
pd
.
PeriodIndex
,
unique
=
False
subtypes
=
(
DatetimeItype
.
subtypes
+
NumericItype
.
subtypes
+
OtherItype
.
subtypes
+
# pd.MultiIndex, not supported
)
())
__itypedict
=
{
DatetimeItype
.
name
:
DatetimeItype
,
IntegerItype
.
name
:
IntegerItype
,
FloatItype
.
name
:
FloatItype
,
NumericItype
.
name
:
NumericItype
,
OtherItype
.
name
:
OtherItype
,
MixedItype
.
name
:
MixedItype
,
}
def
__is_itype_like
(
itype
,
obj
):
"""
Check if obj is a subclass or a instance of the given itype or any of its subtypes
"""
# user gave a string, like 'datetime'
if
isinstance
(
obj
,
str
)
and
obj
==
itype
.
name
:
return
True
def
is_itype
(
obj
,
itype
):
"""
Check if obj is a instance of the given itype or its str-alias was given
"""
# user gave a Itype, like ``DatetimeItype``
if
issubclass
(
obj
,
itype
):
return
True
# user gave a string, like 'datetime'
if
isinstance
(
obj
,
str
)
and
obj
==
itype
.
name
:
return
True
return
False
# check subtypes
# user gave a instance of a subtype, like ``pd.Series(..).index``
if
isinstance
(
obj
,
itype
.
subtypes
):
return
True
def
is_itype_subtype
(
obj
,
itype
):
"""
Check if obj is a subclass or a instance of a subclass of the given itype
"""
# user gave a subtype, like ``pd.DatetimeIndex``
if
issubclass
(
obj
,
itype
.
subtypes
):
return
True
# user gave a instance of a subtype, like ``pd.Series(..).index``
if
isinstance
(
obj
,
itype
.
subtypes
):
return
True
return
False
def
is_itype_like
(
obj
,
itype
):
"""
Check if obj is a subclass or a instance of the given itype or any of its subtypes
"""
return
is_itype
(
obj
,
itype
)
or
is_itype_subtype
(
obj
,
itype
)
def
get_itype
(
obj
):
"""
handle
any possible user input, like
Return the according Itype, by any of
any possible user input, like
-
"
datetime
"
- DatetimeItype
- pd.Series(...).index
- pd.DatetimeIndex
and return the according Itype
"""
t
=
DatetimeItype
if
__is_itype_like
(
t
):
return
t
t
=
NumericItype
if
__is_itype_like
(
t
):
numtypes
=
[
IntegerItype
,
FloatItype
,
MixedItype
]
for
t
in
types
:
if
__is_itype_like
(
t
):
return
t
return
t
raise
ValueError
(
f
"
{
type
(
obj
)
}
is not a indextype nor any known subtype of pd.Index
"
)
# shortcut
if
issubclass
(
obj
,
__Itype
):
return
obj
# check if it is the actual type, not a subtype
types
=
[
DatetimeItype
,
NumericItype
,
IntegerItype
,
FloatItype
,
OtherItype
,
MixedItype
]
for
t
in
types
:
if
is_itype
(
obj
,
t
):
return
t
# If the above failed, we try to infer the itype by its subtypes.
# We just check the unique types, because the non-unique are just
# collections of unique subtypes.
for
t
in
types
:
if
is_itype_subtype
(
obj
,
t
)
and
t
.
unique
:
return
t
raise
ValueError
(
f
"
{
obj
}
is not a itype, nor any known subtype of a itype, nor a itype string alias
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment