adriank / ObjectPath

The agile query language for semi-structured data
http://objectpath.org
MIT License
380 stars 93 forks source link

Average Age of date objects #81

Closed TuSKan closed 2 years ago

TuSKan commented 5 years ago

Hello, In my problem I need to compute the average age of a sub population.

{  
    "peoples":[  
        {  
            "name":"Kelly Robertson",
            "birthDate":"1980-05-11 04:22:33"
        },
        {  
            "name":"Trevin Haley",
            "birthDate":"1985-09-12 12:03:10"
        }
    ]
}

It works for

avg(age(dateTime($.peoples.birthDate.*[1],'%Y-%m-%d %H:%M:%S')))

but not for all elements

avg(age(dateTime($.peoples.birthDate,'%Y-%m-%d %H:%M:%S')))
adriank commented 5 years ago

$.peoples.birthDate

is a list, so dateTime won't work. Maybe I should extend map() to accept parameters. I will think about it.

Greetings, Adrian Kalbarczyk

http://kalbarczyk.co

On Wed, Dec 5, 2018 at 3:31 AM Rener Castro notifications@github.com wrote:

Hello, In my problem I need to compute the average age of a sub population.

{ "peoples":[ { "name":"Kelly Robertson", "birthDate":"1980-05-11 04:22:33" }, { "name":"Trevin Haley", "birthDate":"1985-09-12 12:03:10" } ] }

It works for

avg(age(dateTime($.peoples.birthDate.*[1],'%Y-%m-%d %H:%M:%S')))

but not for all elements

avg(age(dateTime($.peoples.birthDate,'%Y-%m-%d %H:%M:%S')))

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/adriank/ObjectPath/issues/81, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKycv3W_m2cH8hnF5yvgWkXGmGPbRIBks5u1y_ygaJpZM4ZB4zN .

TuSKan commented 5 years ago

It could be this way ?

avg(age(map(dateTime, $.peoples.birthDate, args = '%Y-%m-%d %H:%M:%S')))

A map function could be very helpful. Could implement map as:

def map(f, array, args = None):
    return map(lambda x: f(x, args), array)