python-datetime

Pandas `.to_pydatetime()` not working inside a DataFrame

為{幸葍}努か 提交于 2021-02-15 06:28:28
问题 I have strings like '03-21-2019' that I want to convert to the native Python datetime object: that is, of the datetime.datetime type. The conversion is easy enough through pandas : import pandas as pd import datetime as dt date_str = '03-21-2019' pd_Timestamp = pd.to_datetime(date_str) py_datetime_object = pd_Timestamp.to_pydatetime() print(type(py_datetime_object)) with the result <class 'datetime.datetime'> This is precisely what I want, since I want to compute timedelta 's by subtracting

Python convert raw GMT to othertime zone e.g SGT

别说谁变了你拦得住时间么 提交于 2021-02-11 12:22:56
问题 I am trying to convert from GMT to e.g SGT: For example, the value 0348 GMT should be 11:48 am 1059 GMT should be 6:59 pm how do i do this? i have tried: date="03:48" curr = ( dt.datetime.strptime(date, "%H:%M") .astimezone(timezone('Asia/Singapore')) ) print(curr) But I am getting OSError: [Errno 22] Invalid argument 回答1: Assuming you have a naive datetime object which represents UTC: from datetime import datetime, timezone from dateutil import tz now = datetime.now() print(repr(now)) >>>

Pandas P&L rollup to the next business day

烂漫一生 提交于 2021-02-10 17:50:29
问题 I'm having a hard time trying to do this efficiently. I have some stocks and daily P&L info in a dataframe. In reality, I have millions of rows of data so efficiency matters a lot! The Dataframe looks like : ------------------------------- | Date | Security | P&L | ------------------------------- | 2016-01-01 | AAPL | 100 | ------------------------------- | 2016-01-02 | AAPL | 200 | ------------------------------- | 2016-01-03 | AAPL | 300 | ------------------------------- | 2016-01-04 | AAPL

How to get the week number of the current quarter in Python?

老子叫甜甜 提交于 2021-02-10 14:56:17
问题 I've been through every question and every third party library trying to figure out a way to do this where I don't have to manually map dates. I'm trying to get the week number of the current fiscal quarter. Each quarter starts on the 1st of either January, April, July or October. Given a date (string or object, it doesn't matter), I need to be able to calculate the week number of the fiscal quarter that it's in. To make matters a little more complicated, the Fiscal year starts in April. So

How to get the week number of the current quarter in Python?

旧城冷巷雨未停 提交于 2021-02-10 14:54:26
问题 I've been through every question and every third party library trying to figure out a way to do this where I don't have to manually map dates. I'm trying to get the week number of the current fiscal quarter. Each quarter starts on the 1st of either January, April, July or October. Given a date (string or object, it doesn't matter), I need to be able to calculate the week number of the fiscal quarter that it's in. To make matters a little more complicated, the Fiscal year starts in April. So

Python: How to find the nth weekday of the year?

醉酒当歌 提交于 2021-02-08 19:38:07
问题 I have seen a lot of similar posts on "nth weekday of the month", but my question pertains to "nth weekday of the year". Background: I have a table that has daily sales data. There are 3 columns: date, day of week (Mon, Tue, Wed etc.) and sales. I would like to match nth weekday of Year 1 with Year 2 and compare sales that way. Example1: 01/06/2020 matches with 01/04/2021, both are the 1st Monday of that year. Example2: 11/02/2019 matches with 10/31/2020, both are the 44th Saturday of that

Python: How to find the nth weekday of the year?

情到浓时终转凉″ 提交于 2021-02-08 19:36:35
问题 I have seen a lot of similar posts on "nth weekday of the month", but my question pertains to "nth weekday of the year". Background: I have a table that has daily sales data. There are 3 columns: date, day of week (Mon, Tue, Wed etc.) and sales. I would like to match nth weekday of Year 1 with Year 2 and compare sales that way. Example1: 01/06/2020 matches with 01/04/2021, both are the 1st Monday of that year. Example2: 11/02/2019 matches with 10/31/2020, both are the 44th Saturday of that

Why does datetime give different timezone formats for the same timezone?

你说的曾经没有我的故事 提交于 2021-02-08 13:58:11
问题 >>> now = datetime.datetime.now(pytz.timezone('Asia/Tokyo')) >>> dt = datetime(now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond, pytz.timezone('Asia/Tokyo')) >>> now datetime.datetime(2018, 9, 7, 16, 9, 24, 177751, tzinfo=<DstTzInfo 'Asia/Tokyo' JST+9:00:00 STD>) >>> dt = datetime(now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond, pytz.timezone('Asia/Tokyo')) >>> dt datetime.datetime(2018, 9, 7, 16, 9, 24, 177751, tzinfo=<DstTzInfo

datetime.strptime(‘2017-01-12T14:12:06.000-0500’,'%Y-%m-%dT%H:%M:%S.%f%Z')

怎甘沉沦 提交于 2021-02-07 03:54:08
问题 I've been trying to convert this specific date format to a string in Python like so: datetime.strptime(‘2017-01-12T14:12:06.000-0500’,'%Y-%m-%dT%H:%M:%S.%f%Z') But it doesn't work. What am I doing wrong? 回答1: Solution for Python 2.7 From the comments it became clear that OP needs a solution for Python 2.7. Apparently, there's no %z in strptime for python 2.7 even though the documentation claims the contrary, the raised error is ValueError: 'z' is a bad directive in format '%Y-%m-%dT%H:%M:%S

datetime.strptime(‘2017-01-12T14:12:06.000-0500’,'%Y-%m-%dT%H:%M:%S.%f%Z')

坚强是说给别人听的谎言 提交于 2021-02-07 03:53:19
问题 I've been trying to convert this specific date format to a string in Python like so: datetime.strptime(‘2017-01-12T14:12:06.000-0500’,'%Y-%m-%dT%H:%M:%S.%f%Z') But it doesn't work. What am I doing wrong? 回答1: Solution for Python 2.7 From the comments it became clear that OP needs a solution for Python 2.7. Apparently, there's no %z in strptime for python 2.7 even though the documentation claims the contrary, the raised error is ValueError: 'z' is a bad directive in format '%Y-%m-%dT%H:%M:%S