filtering a list of lists of dates in python
I have the following list of list:
[[Timestamp('2018-04-07 00:00:00'), Timestamp('2018-07-07 00:00:00'),Timestamp('2020-04-07 00:00:00')], [Timestamp('2018-04-07 00:00:00'),Timestamp('2018-10-07 00:00:00'),Timestamp('2018-12-07 00:00:00'),Timestamp('2021-04-07 00:00:00'),Timestamp('2022-04-07 00:00:00'),Timestamp('2023-04-07 00:00:00')]
And my question is if it is possible filter the list of lists by another date. For example:
If I choose this date:
datetime.datetime(2019, 1, 26, 0, 0)
Is there a way to filter the list of lists to just taking those dates that are higher than the date I choose and also the last one before my date?
For example for the first list of the lists I want to keep the values:
[Timestamp('2018-07-07 00:00:00'),Timestamp('2020-04-07 00:00:00')]
And for the second list of the list of lists:
[Timestamp('2018-12-07 00:00:00'),Timestamp('2021-04-07 00:00:00'),Timestamp('2022-04-07 00:00:00'),Timestamp('2023-04-07 00:00:00')]
python python-3.x datetime timestamp filtering
add a comment |
I have the following list of list:
[[Timestamp('2018-04-07 00:00:00'), Timestamp('2018-07-07 00:00:00'),Timestamp('2020-04-07 00:00:00')], [Timestamp('2018-04-07 00:00:00'),Timestamp('2018-10-07 00:00:00'),Timestamp('2018-12-07 00:00:00'),Timestamp('2021-04-07 00:00:00'),Timestamp('2022-04-07 00:00:00'),Timestamp('2023-04-07 00:00:00')]
And my question is if it is possible filter the list of lists by another date. For example:
If I choose this date:
datetime.datetime(2019, 1, 26, 0, 0)
Is there a way to filter the list of lists to just taking those dates that are higher than the date I choose and also the last one before my date?
For example for the first list of the lists I want to keep the values:
[Timestamp('2018-07-07 00:00:00'),Timestamp('2020-04-07 00:00:00')]
And for the second list of the list of lists:
[Timestamp('2018-12-07 00:00:00'),Timestamp('2021-04-07 00:00:00'),Timestamp('2022-04-07 00:00:00'),Timestamp('2023-04-07 00:00:00')]
python python-3.x datetime timestamp filtering
add a comment |
I have the following list of list:
[[Timestamp('2018-04-07 00:00:00'), Timestamp('2018-07-07 00:00:00'),Timestamp('2020-04-07 00:00:00')], [Timestamp('2018-04-07 00:00:00'),Timestamp('2018-10-07 00:00:00'),Timestamp('2018-12-07 00:00:00'),Timestamp('2021-04-07 00:00:00'),Timestamp('2022-04-07 00:00:00'),Timestamp('2023-04-07 00:00:00')]
And my question is if it is possible filter the list of lists by another date. For example:
If I choose this date:
datetime.datetime(2019, 1, 26, 0, 0)
Is there a way to filter the list of lists to just taking those dates that are higher than the date I choose and also the last one before my date?
For example for the first list of the lists I want to keep the values:
[Timestamp('2018-07-07 00:00:00'),Timestamp('2020-04-07 00:00:00')]
And for the second list of the list of lists:
[Timestamp('2018-12-07 00:00:00'),Timestamp('2021-04-07 00:00:00'),Timestamp('2022-04-07 00:00:00'),Timestamp('2023-04-07 00:00:00')]
python python-3.x datetime timestamp filtering
I have the following list of list:
[[Timestamp('2018-04-07 00:00:00'), Timestamp('2018-07-07 00:00:00'),Timestamp('2020-04-07 00:00:00')], [Timestamp('2018-04-07 00:00:00'),Timestamp('2018-10-07 00:00:00'),Timestamp('2018-12-07 00:00:00'),Timestamp('2021-04-07 00:00:00'),Timestamp('2022-04-07 00:00:00'),Timestamp('2023-04-07 00:00:00')]
And my question is if it is possible filter the list of lists by another date. For example:
If I choose this date:
datetime.datetime(2019, 1, 26, 0, 0)
Is there a way to filter the list of lists to just taking those dates that are higher than the date I choose and also the last one before my date?
For example for the first list of the lists I want to keep the values:
[Timestamp('2018-07-07 00:00:00'),Timestamp('2020-04-07 00:00:00')]
And for the second list of the list of lists:
[Timestamp('2018-12-07 00:00:00'),Timestamp('2021-04-07 00:00:00'),Timestamp('2022-04-07 00:00:00'),Timestamp('2023-04-07 00:00:00')]
python python-3.x datetime timestamp filtering
python python-3.x datetime timestamp filtering
asked Nov 15 '18 at 11:40
AmartinAmartin
569
569
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Something like this?
import datetime
from pandas import Timestamp
# List of Timestamps
list_of_dates = [[Timestamp('2018-04-07 00:00:00'), Timestamp('2018-07-07 00:00:00'), Timestamp('2020-04-07 00:00:00')], [Timestamp('2018-04-07 00:00:00'), Timestamp('2018-10-07 00:00:00'), Timestamp('2018-12-07 00:00:00'), Timestamp('2021-04-07 00:00:00'), Timestamp('2022-04-07 00:00:00'), Timestamp('2023-04-07 00:00:00')]]
# Target date that we use as filter
target_date = datetime.datetime(2019, 1, 26, 0, 0)
def filter_dates(date_list, target_date):
"""Filter given timestamps according to target date.
Keep last date before target date and all future dates after target date."""
# Initialise return list
filtered_dates =
# Iterate over list of lists
for dates in date_list:
# Use list comprehension to filter dates that are on either sides of the target date
dates_before_target_date = [date for date in dates if date < target_date]
dates_after_target_date = [date for date in dates if date > target_date]
# Keep last date before the target date and all future dates
filtered_dates.append([dates_before_target_date[-1]] + dates_after_target_date)
return filtered_dates
filtered_dates = filter_dates(list_of_dates, target_date)
print(filtered_dates)
This produces
[
[Timestamp('2018-07-07 00:00:00'), Timestamp('2020-04-07 00:00:00')],
[Timestamp('2018-12-07 00:00:00'), Timestamp('2021-04-07 00:00:00'), Timestamp('2022-04-07 00:00:00'), Timestamp('2023-04-07 00:00:00')]
]
Yes it works. Thank you so much
– Amartin
Nov 15 '18 at 13:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318697%2ffiltering-a-list-of-lists-of-dates-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Something like this?
import datetime
from pandas import Timestamp
# List of Timestamps
list_of_dates = [[Timestamp('2018-04-07 00:00:00'), Timestamp('2018-07-07 00:00:00'), Timestamp('2020-04-07 00:00:00')], [Timestamp('2018-04-07 00:00:00'), Timestamp('2018-10-07 00:00:00'), Timestamp('2018-12-07 00:00:00'), Timestamp('2021-04-07 00:00:00'), Timestamp('2022-04-07 00:00:00'), Timestamp('2023-04-07 00:00:00')]]
# Target date that we use as filter
target_date = datetime.datetime(2019, 1, 26, 0, 0)
def filter_dates(date_list, target_date):
"""Filter given timestamps according to target date.
Keep last date before target date and all future dates after target date."""
# Initialise return list
filtered_dates =
# Iterate over list of lists
for dates in date_list:
# Use list comprehension to filter dates that are on either sides of the target date
dates_before_target_date = [date for date in dates if date < target_date]
dates_after_target_date = [date for date in dates if date > target_date]
# Keep last date before the target date and all future dates
filtered_dates.append([dates_before_target_date[-1]] + dates_after_target_date)
return filtered_dates
filtered_dates = filter_dates(list_of_dates, target_date)
print(filtered_dates)
This produces
[
[Timestamp('2018-07-07 00:00:00'), Timestamp('2020-04-07 00:00:00')],
[Timestamp('2018-12-07 00:00:00'), Timestamp('2021-04-07 00:00:00'), Timestamp('2022-04-07 00:00:00'), Timestamp('2023-04-07 00:00:00')]
]
Yes it works. Thank you so much
– Amartin
Nov 15 '18 at 13:52
add a comment |
Something like this?
import datetime
from pandas import Timestamp
# List of Timestamps
list_of_dates = [[Timestamp('2018-04-07 00:00:00'), Timestamp('2018-07-07 00:00:00'), Timestamp('2020-04-07 00:00:00')], [Timestamp('2018-04-07 00:00:00'), Timestamp('2018-10-07 00:00:00'), Timestamp('2018-12-07 00:00:00'), Timestamp('2021-04-07 00:00:00'), Timestamp('2022-04-07 00:00:00'), Timestamp('2023-04-07 00:00:00')]]
# Target date that we use as filter
target_date = datetime.datetime(2019, 1, 26, 0, 0)
def filter_dates(date_list, target_date):
"""Filter given timestamps according to target date.
Keep last date before target date and all future dates after target date."""
# Initialise return list
filtered_dates =
# Iterate over list of lists
for dates in date_list:
# Use list comprehension to filter dates that are on either sides of the target date
dates_before_target_date = [date for date in dates if date < target_date]
dates_after_target_date = [date for date in dates if date > target_date]
# Keep last date before the target date and all future dates
filtered_dates.append([dates_before_target_date[-1]] + dates_after_target_date)
return filtered_dates
filtered_dates = filter_dates(list_of_dates, target_date)
print(filtered_dates)
This produces
[
[Timestamp('2018-07-07 00:00:00'), Timestamp('2020-04-07 00:00:00')],
[Timestamp('2018-12-07 00:00:00'), Timestamp('2021-04-07 00:00:00'), Timestamp('2022-04-07 00:00:00'), Timestamp('2023-04-07 00:00:00')]
]
Yes it works. Thank you so much
– Amartin
Nov 15 '18 at 13:52
add a comment |
Something like this?
import datetime
from pandas import Timestamp
# List of Timestamps
list_of_dates = [[Timestamp('2018-04-07 00:00:00'), Timestamp('2018-07-07 00:00:00'), Timestamp('2020-04-07 00:00:00')], [Timestamp('2018-04-07 00:00:00'), Timestamp('2018-10-07 00:00:00'), Timestamp('2018-12-07 00:00:00'), Timestamp('2021-04-07 00:00:00'), Timestamp('2022-04-07 00:00:00'), Timestamp('2023-04-07 00:00:00')]]
# Target date that we use as filter
target_date = datetime.datetime(2019, 1, 26, 0, 0)
def filter_dates(date_list, target_date):
"""Filter given timestamps according to target date.
Keep last date before target date and all future dates after target date."""
# Initialise return list
filtered_dates =
# Iterate over list of lists
for dates in date_list:
# Use list comprehension to filter dates that are on either sides of the target date
dates_before_target_date = [date for date in dates if date < target_date]
dates_after_target_date = [date for date in dates if date > target_date]
# Keep last date before the target date and all future dates
filtered_dates.append([dates_before_target_date[-1]] + dates_after_target_date)
return filtered_dates
filtered_dates = filter_dates(list_of_dates, target_date)
print(filtered_dates)
This produces
[
[Timestamp('2018-07-07 00:00:00'), Timestamp('2020-04-07 00:00:00')],
[Timestamp('2018-12-07 00:00:00'), Timestamp('2021-04-07 00:00:00'), Timestamp('2022-04-07 00:00:00'), Timestamp('2023-04-07 00:00:00')]
]
Something like this?
import datetime
from pandas import Timestamp
# List of Timestamps
list_of_dates = [[Timestamp('2018-04-07 00:00:00'), Timestamp('2018-07-07 00:00:00'), Timestamp('2020-04-07 00:00:00')], [Timestamp('2018-04-07 00:00:00'), Timestamp('2018-10-07 00:00:00'), Timestamp('2018-12-07 00:00:00'), Timestamp('2021-04-07 00:00:00'), Timestamp('2022-04-07 00:00:00'), Timestamp('2023-04-07 00:00:00')]]
# Target date that we use as filter
target_date = datetime.datetime(2019, 1, 26, 0, 0)
def filter_dates(date_list, target_date):
"""Filter given timestamps according to target date.
Keep last date before target date and all future dates after target date."""
# Initialise return list
filtered_dates =
# Iterate over list of lists
for dates in date_list:
# Use list comprehension to filter dates that are on either sides of the target date
dates_before_target_date = [date for date in dates if date < target_date]
dates_after_target_date = [date for date in dates if date > target_date]
# Keep last date before the target date and all future dates
filtered_dates.append([dates_before_target_date[-1]] + dates_after_target_date)
return filtered_dates
filtered_dates = filter_dates(list_of_dates, target_date)
print(filtered_dates)
This produces
[
[Timestamp('2018-07-07 00:00:00'), Timestamp('2020-04-07 00:00:00')],
[Timestamp('2018-12-07 00:00:00'), Timestamp('2021-04-07 00:00:00'), Timestamp('2022-04-07 00:00:00'), Timestamp('2023-04-07 00:00:00')]
]
answered Nov 15 '18 at 12:45
TeemuTeemu
686
686
Yes it works. Thank you so much
– Amartin
Nov 15 '18 at 13:52
add a comment |
Yes it works. Thank you so much
– Amartin
Nov 15 '18 at 13:52
Yes it works. Thank you so much
– Amartin
Nov 15 '18 at 13:52
Yes it works. Thank you so much
– Amartin
Nov 15 '18 at 13:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318697%2ffiltering-a-list-of-lists-of-dates-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown