Rails Previous Sunday in Relation to Any DateTime

Rails has a convenience method for calculating the beginning of the week. irb(main):001:0> Date.today.beginning_of_week(:sunday) #=> Sun, 16 Dec 2012 See the Date Rails extensions for other possibilities: http://api.rubyonrails.org/classes/Date.html Update: Rails 5.2 adds convenience methods to find the next/previous occurrence of a day. [1] pry(main)> Date.today.prev_occurring(:sunday) #=> Sun, 29 Apr 2018 See the DateAndTime::Calculations documentation for … Read more

Pandas: Change day

You can use .apply and datetime.replace, eg: import pandas as pd from datetime import datetime ps = pd.Series([datetime(2014, 1, 7), datetime(2014, 3, 13), datetime(2014, 6, 12)]) new = ps.apply(lambda dt: dt.replace(day=1)) Gives: 0 2014-01-01 1 2014-03-01 2 2014-06-01 dtype: datetime64[ns]

Convert date from excel in number format to date format python [duplicate]

from datetime import datetime excel_date = 42139 dt = datetime.fromordinal(datetime(1900, 1, 1).toordinal() + excel_date – 2) tt = dt.timetuple() print(dt) print(tt) As mentioned by J.F. Sebastian, this answer only works for any date after 1900/03/01 EDIT: (in answer to @R.K) If your excel_date is a float number, use this code: from datetime import datetime def … Read more

need to convert UTC (aws ec2) to PST in Python

from datetime import datetime from pytz import timezone import pytz date_format=”%m/%d/%Y %H:%M:%S %Z” date = datetime.now(tz=pytz.utc) print ‘Current date & time is:’, date.strftime(date_format) date = date.astimezone(timezone(‘US/Pacific’)) print ‘Local date & time is :’, date.strftime(date_format) seems to work for me 🙂 – timezones are confusing, slowly making a plan of what I actually want to do … Read more

AttributeError: ‘module’ object has no attribute ‘utcnow’

You are confusing the module with the type. Use either: import datetime datetime.datetime.utcnow() or use: from datetime import datetime datetime.utcnow() e.g. either reference the datetime type in the datetime module, or import that type into your namespace from the module. If you use the latter form and need other types from that module, don’t forget … Read more

Create a day-of-week column

Pandas 0.23+ Use pandas.Series.dt.day_name(), since pandas.Timestamp.weekday_name has been deprecated: import pandas as pd df = pd.DataFrame({‘my_dates’:[‘2015-01-01′,’2015-01-02′,’2015-01-03′],’myvals’:[1,2,3]}) df[‘my_dates’] = pd.to_datetime(df[‘my_dates’]) df[‘day_of_week’] = df[‘my_dates’].dt.day_name() Output: my_dates myvals day_of_week 0 2015-01-01 1 Thursday 1 2015-01-02 2 Friday 2 2015-01-03 3 Saturday Pandas 0.18.1+ As user jezrael points out below, dt.weekday_name was added in version 0.18.1 Pandas Docs import … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)